Enabling Layout in Zend Framework

See Category:Go images for sourcecode and usag...
Image via Wikipedia

To maintain a consistent look and feel through out your website, Zend_Layout provides layout facility by which you can create single or multiple document structures as like as templates.

Enabling Zend_Layout means informing bootstrap to use Layout resource. You can enable layout with the help of zf cli tools from terminal as:


$ zf enable layout

Layout has been enabled, and a default layout created at application/layouts/scripts/layout.phtml. A layout entry has been added to the application config file.

With the above command you will have following things ready:

–       application/configs/application.ini is updated; contains following line at your production section


resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

–       A layout file created at /application/layouts/scripts/layout.phtml

Also you need to initialize the View resource; add the following line to your have application/configs/application.ini


; Add to [production] section:

resources.view[] =

Now to run view resource we will add just a little code at bootstrap:


class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

protected function _initView(){

$this->bootstrap('view');

$view = $this->getResource('view');

$view->doctype('XHTML1_STRICT'); //can set the doc type here

}

}

Now  Zend_Layout is ready to go; may modify the layout or add some styles. Just don’t forget to echo your content i.e. $this->layout()->content  at your layout body to display your content or view html segments:


<!-- application/layouts/scripts/layout.phtml -->

<?php echo $this->doctype() ?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>my title</title>

</head>

<body>

<?php echo $this->layout()->content ?>

</body>

</html>

It’s a go!

if you need to use multiple layouts, you may read my next post on multiple layout usage