Zendframework entry 2-Use Layout

Source: Internet
Author: User

You may have noticed that the view scripts in the previous sections were HTML fragments-not complete pages. this is by design; we want our actions to return content only related to the action itself, not the application as a whole.

You may have noticed that the view scripts in the previous section are all HTML fragments and are not complete pages. This is designed on purpose because we want the actions to only return their own content, rather than the entire application.Program.

Now we must compose that generated content into a full HTML page. we 'd also like to have a consistent look and feel for the application. we will use a global site layout to accomplish both of these tasks.

Now we have to assemble the generated content into a complete HTML page, and we want the appearance and feeling of the entire program to be consistent, therefore, we will use the global site layout to complete these two tasks.

There are two design patterns that Zend framework uses to implement layouts: Two Step view and composite view.Two Step ViewIs usually associated with the transform view pattern; the basic idea is that your application view creates a representation that is then injected into the master view for final transformation.Composite ViewPattern deals with a view made of one or more atomic, application views.

ZF has two design modes: Two-Step view and combined view. The two-step view is usually associated with the conversion view mode. The basic idea is to create a content in your program during the final conversion and inject it into the main view.

In Zend framework, zend_layout combines the ideas behind these patterns. Instead of each action View Script needing to include site-wide artifacts, they can simply focus on their own responsibilities.

In ZF, zend_layout combines the two modes. Unlike every action attempt script that contains parts of a global site, they simply focus on their responsibilities.

Occasionally, however, you may need application-specific information in your site-wide view script. Fortunately, Zend framework provides a variety of ViewPlaceholdersTo allow you to provide such information from your action view scripts.

Sometimes you need to put specific application information into your full site View Script. Fortunately, ZF provides various view placeholders for your action attempt scripts.

To get started using zend_layout, first we need to inform our Bootstrap to use the layout resource. This can be done usingZF enable LayoutCommand:

To start using zend_layout, the prime minister needs to notify Bootstrap to use layout resources. We use the ZF enable layout command.

 
1:% ZF enable Layout

 
2:

3:Layouts have been enabled,AndADefaultLayout created

 
4:

 
5:Application/layouts/scripts/layout. phtml

A Layout entry has been added to the application config file.

The layout has been added to the application configuration file.

As noted by the command, application/configs/application. INI is updated, and now contains the following within the Production Section:

As indicated by the command, application/configs/application. INI is updated and contains the following information in the Production Section:

1:; Application/configs/application. ini

 
2:; Add to [production] section:

 
3:Resources. layout. layoutpath = application_path"/Layouts/scripts"

The final INI file shoshould look as follows:

The final INI file is as follows:

 
1:; Application/configs/application. ini

 
2:[Production]

 
3:; PHP settings we want to initialize

 
4:Phpsettings. display_startup_errors = 0

 
5:Phpsettings. display_errors = 0

 
6:Includepaths. Library = application_path"/../Library"

 
7:Bootstrap. Path = application_path"/Bootstrap. php"

 
8:Bootstrap.Class="Bootstrap"

9:Appnamespace ="Application"

 
10:Resources. frontcontroller. controllerdirectory = application_path"/Controllers"

 
11:Resources. frontcontroller. Params. displayexceptions = 0

 
12:Resources. layout. layoutpath = application_path"/Layouts/scripts"

 
13:[Staging: Production]

14:[Testing: Production]

 
15:Phpsettings. display_startup_errors = 1

 
16:Phpsettings. display_errors = 1

 
17:[Development: Production]

 
18:Phpsettings. display_startup_errors = 1

 
19:Phpsettings. display_errors = 1

This directive tells your application to look for Layout View scripts in application/layouts/scripts. if you examine your directory tree, you'll see that this directory has been created for you now, with the file layout. phtml.

This command tells your application to find the Layout View Script from application/layouts/scripts. If you view the directory tree, you will find that the directory has been created, and the layout. phtml file is also in it.

We also want to ensure we have An XHTML doctype Declaration for our application. To enable this, we need to add a resource to our Bootstrap.

We also want to confirm that our application already has the XHTML doctype declaration. To enable this function, we need to add a resource to bootstrap.

The simplest way to add a bootstrap resource is to simply create a protected method beginning with the phrase _ init. in this case, we want to initialize the doctype, so we'll create an _ initdoctype () method within our Bootstrap class:

The simplest way to add a bootstrap resource is to create a protected method starting with _ init. We want to initialize doctype. In this case, we will create a _ initdoctype () method in the bootstrap class.

1: // Application/Bootstrap. php

 
2: ClassBootstrapExtendsZend_application_bootstrap_bootstrap

 
3:{

 
4:

 
5:Protected Function_ Initdoctype ()

 
6:{

7:

 
8:}

 
9:}

Within that method, we need to hint to the view to use the appropriate doctype. But where will the view object come from? The easy solution is to initialize the view resource; once we have, we can pull the view object from the bootstrap and use it.

In this method, we need to prompt the view to use the appropriate doctype. But where does the view object come from? The simple method is to initialize the view resource. Once we define the view resource, we can get the view object from Bootstrap and use it.

To initialize the view resource, add the following line to your application/configs/application. ini file, in the section marked production:

To initialize view resources, add the following content to the Production Section of application/configs/application. ini:

1:; Application/configs/application. ini

 
2:; Add to [production] section:

 
3:Resources. view [] =

This tells us to initialize the view with no options (the '[]' indicates that the "View" Key is an array, and we pass nothing to it ).

This indicates that the view is initialized and there is no parameter ([] indicates that the view key is an array, and no parameter is passed here ).

Now that we have a view, let's flesh out our _ initdoctype () method. in it, we will first ensure the view resource has run, fetch the view object, and then configure it:

Now that we have a view, we can implement the _ initdoctype () method. InCodeFirst, check whether the view resource is running, get the view object, and configure it.

1: // Application/Bootstrap. php

 
2: ClassBootstrapExtendsZend_application_bootstrap_bootstrap

 
3:{

 
4:

 
5:Protected Function_ Initdoctype ()

 
6:{

7:$ This-> Bootstrap ('View');

 
8:$ View = $ this-> getresource ('View');

 
9:$ View-> doctype ('Html1 _ strict');

 
10:}

 
11:}

Now that we 've initialized zend_layout and set the doctype, let's create our site-wide layout:

Now we have initialized zend_layout and set doctype. Next we will create a full site layout:

1:<! -- Application/layouts/scripts/layout. phtml -->

 
2:<? PHPEcho$ This-> doctype ()?>

 
3:<HTML xmlns =Http://www.w3.org/1999/xhtml">

 
4:<Head>

 
5:<Meta http-equiv ="Content-Type"Content ="Text/html; charset = UTF-8"/>

6:<Title> Zend framework Quickstart application </title>

 
7:<? PHPEcho$ This-> headlink ()-> appendstylesheet ('/CSS/global.css')?>

 
8:</Head>

 
9:

 
10:<Body>

11:<Div id ="Header"Style ="Background-color: # eeeeee; Height: 30px ;">

 
12:<Div id ="Header-logo"Style ="Float: left">

 
13:<B> ZF Quickstart application </B>

 
14:</Div>

15:<Div id ="Header-navigation"Style ="Float: Right">

 
16:<A href ="<? PHP echo $ this-> URL (

 
17:Array ('controller' => 'guestbook '),

 
18:'Default ',

 
19:True)?> "> Guestbook </a>

20:</Div>

 
21:</Div>

 
22:<? PHPEcho$ This-> layout ()-> content?>

 
23:</Body>

 
24:</Html>

We grab our application content using the layout () view helper, and accessing the "content" key. you may render to other response segments if you wish to, but in most cases, this is all that's necessary.

We extract the application content to use the layout () view assistant and access its content attributes. You can also add other respone segments if you like, which is usually all.

Note also the use of the headlink () placeholder. this is an easy way to generate the HTML for <link> elements, as well as to keep track of them throughout your application. if you need to add additional CSS sheets to support a single action, you can do so, and be assured it will be present in the final rendered page.

Note that the headlink () placeholder is used. This is a simple way to generate HTML <link> elements. If you need to add an additional CSS file to support a single action, you can do so and parse it to the final page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.