Advanced layout in Zend Framework

Why i am calling it advanced Layout of  Zend Framework

  • We can add different Javascript to head section of different Controller/action
  • We can have different page title of different page action
  • We can have different css file for different view page.

Yes I am talking about the composite view of Layout.Composite layout design really overcome the drawbacks of two step design view pattern.

Files that are needed to Solve our problem

1.Bootstrap.php

2.application.ini

3.layout.phtml

4.yourOwnViewPage.phtml

layout.phtml , it could be any name that you are using for your template.If you are not created 1 yet then please check my previous post on layout design using zend framework.

Bootstrap.php

G0to bootstrap.php and add following lines

1
2
3
4
5
6
7
8
9
10
11
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
  protected function _initHeaderScript()
  {
     $this->bootstrap('View');
     $view = $this->getResource('View');
     $view->placeholder('headerScript')
          ->setPrefix('');
  }
}

What i have done here, Created a function called HeaderScript() and added keyword called _init, which will help it to autometically fire itself.Then Added a placehoder named as headerScript.My intension is to add dynamic javascript to different view files.

application.ini

Somes times it might showing some error ,so to avoid that we have add 1 line of code to overcome that

1
resources.view[] = ""

layout.phtml

As my intension is to add uniq javascript files for different view.So i should put the placeholder at the head section of the html layout in our template file called layout.phtml.
<html>
<head>
<title>My Site</title>
<?php echo $this->placeholder(‘headerScript’) ?>
</head>
<body>
<table width=”1024px” align=”center” border=”1″ bgcolor=”red”>
<tr>
<td><?php echo $this->layout()->content ?></td>
</tr>
</table>
</body>
</html>

 

yourOwnViewPage.phtml

1
2
$this->placeholder('headerScript')
->append('<script src="xyz.js" type="text/javascript"><!--mce:0--></script>');

this 1 line code in view page will dynamically add the xyz.js javascript file to the Template’s head section.

 

Next is what

Use this placeholder technique of Zend Framework to Design Dynamic menu system for the different View page.

Uniq CSS file for different view page.Dynamic Sidebar,footer and etc.