Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Other Operations

In most cases, the above configuration and layout script (with modifications) will get you what you need. However, some other functionality exists you will likely use sooner or later. In all of the following examples, you may use one of the methods listed above for retrieving the layout object.

  • Setting layout variables. Zend_Layout keeps its own registry of layout-specific view variables that you can access; the $content key noted in the initial layout script sample is one such example. You can assign and retrieve these using normal property access, or via the assign() method.

    // Setting content:
    $layout->somekey = "foo"
    // Echoing that same content:
    echo $layout->somekey; // 'foo'
    // Using the assign() method:
    $layout->assign('someotherkey', 'bar');
    // Access to assign()'d variables remains the same:
    echo $layout->someotherkey; // 'bar'
  • disableLayout(). Occasionally, you may want to disable layouts; for example, when answering an Ajax request, or providing a RESTful representation of a resource. In these cases, you can call the disableLayout() method on your layout object.


    The opposite of this method is, of course, enableLayout(), which can be called at any time to re-enable layouts for the requested action.

  • Selecting an alternate layout: If you have multiple layouts for your site or application, you can select the layout to use at any time by simply calling the setLayout() method. Call it by specifying the name of the layout script without the file suffix.

    // Use the layout script "alternate.phtml":

    The layout script should reside in the $layoutPath directory specified in your configuration. Zend_Layout will then use this new layout when rendering.

Zend Framework