Skip Navigation LinksHome > Categories > Code from a Category

Zend_Layout - it's easy! Template projects.

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1859
Add Date: 06/19/2012
Zend_Layout - a critical component of Zend Framework. The component is very simple, but many do not properly understand its purpose, and either does not know what it is. So, this article simply and effectively explains the following:

  • What Zend_Layout;
  • For what it is;
  • What is the difference between the Zend_Layout and Zend_View;
  • Real examples.

    What Zend_Layout and what is it for?

    Zend_Layout - a powerful tool in the hands of a professional programmer.
    This tool allows you to clearly divide your logic View into two parts, templates and scripts of the form. What does this give you?

    1.You get a transparent mechanism for templating;
    2. You will be able to avoid a repetition of code in your view scripts;
    3. Get the ability to change templates on the fly;
    4. Can a more convenient way to use the head * view helpers;

    Therefore, for most projects, I strongly recommend you to use Zend_Layout.
    Of course, all the power and convenience of this approach is no article will explain to you - you should definitely do everything to try!

    What is the difference between the Zend_Layout and Zend_View

    When using Zend_View, you probably create a few scripts in order to use the same design for the pages of the same type, then you connect these scripts into specific types of scripts, putting out all the content, is not it?

    Zend_Layout doing these operations for you! You create a script template, then the script.

    Zend_Layout collects for you the template and inserts the srenderenny script in a place specified by you (if it is not necessary to assume that Zend_View does not work because it is not. Zend_View It renders as ever your view scripts, and Zend_Layout Ā«intercepts "them sticking to the template).

    This is a very useful tool, especially when designing large portal projects. Well, I'm already interested in you? Then read on and I'll tell you in the form of practical examples of how all this stuff to use.

    Actual examples of use.

    So, let's try all the above into practice. Let's start with your boot file (bootstrap). To enable Zend_Layout you need to configure this component. Enter the following lines to your bootstrap (prior to calling dispatch () front-controller):

    To be honest this is not the only way to configure Zend_Layout, but the most convenient.

    With the help of configuration options (see manual) can be much change in the template, for example, change the extension of your template phtml to the tpl. This is very simply and effectively painted in the manual, so I will not stop there, because you need to explain the more important things.

    Now you need to write a script template. It's easier than you think. So, take these steps:

    1.Open the folder you specified in the bootstrap;
    2. Create a file index.phtml;
    3. Open the file index.phtml test and insert the following:

    In this example, the active use of the helper type, whose value in the template you will surely appreciate later. In the template, you will also work in the context of Zend_View, which means that the $ this variable is an object Zend_View. $ this-> layout () - is an object Zend_Layout, where $ this-> layout () -> content - the content type of script.

    You can also store the variables available from any kind of script with Zend_Layout.

    The principle of creating scripts is the same, only now you do not need to connect a bunch of different header and footer scripts.
    Here is an example of a simple script like:

    Now, if we get into the controller and action, for which we have created a script form, the browser will return us to the following code:

    That is, As you can see, at first rendered view script, and then render the template, and is inserted into a template for rendering the contents of the view.

    And if you look at the view helpers used in the examples, you will better understand the usefulness of this approach.

    Thank you for your attention.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.