views:

192

answers:

5

Hi guys,

So I had a question on general organization of code for the Zend framework with regard to the layout.

My layout is basically this:

(LAYOUT.PHTML)

<div id='header'>
<?= $this->Layout()->header ?>
</div>

<div id='main'>
<?= $this->Layout()->main ?>
</div>

<div id='footer'>
<?= $this->Layout()->footer ?>
</div>

and so on and so forth. Now, in order to keep my code in my header separate from the code of my main and the code of my footer, I've created a folder for my view that holds header.phtml, main.phtml, footer.phtml. I then use this code to assign the content of header.phtml into $this->layout()->header:

(INDEX.PHTML)

$this->Layout()->header = file_get_contents('index/header.phtml');
$this->Layout()->main = file_get_contents('index/main.phtml');
$this->Layout()->footer = file_get_contents('index/footer.phtml');

That was working great, but I've hit a point where I don't want main to be static HTML anymore. I would like to be able to insert some values with PHP. So in my Controller in indexAction, I want to be able to load from my database and put values into index/main.phtml. Is there a way to do this without restructuring my site?

If not is there a way to do it so that I can have:

  1. The ability to put code into different sections of my layout, such as Layout()->header, Layout->footer.

  2. Separate these pieces into different files, so that they're easy to find and organize, like my index/footer.phtml, index/main.phtml etc.

  3. Not have to put that code into quotes unnecessarily to turn it into a string to pass it to Layout()->header etc.

Thank you guys so much for your help.

-Ethan

+1  A: 
Byron Whitlock
You, sir, are the goldenest of gods
Ethan
A: 

Have you tried looking at view helpers. They are a way of structuring view logic into reusable and modular code. In this case you would use a view helper to generate each of your required segments. So your example view script would look like

$this->Layout()->header = $this->header();
$this->Layout()->main = $this->main();
$this->Layout()->footer = $this->footer();

The benefit of using view helpers over include and require statements is that all of the file handling and name resolution is handled by the framework. The manual has more information on how to set up the paths and usage examples etc.

Tim Wardle
A: 

helpers are good. Another option is like the above, putting filenames in header/footer - put the template names and use $this->render($this->layout()->header)), etc etc. This is just like the include/require above, but more consistent.

Justin
+1  A: 

The ->header in $this->layout()->header is response segment. You can render parts of response using $this->_helper->viewRenderer->setResponseSegment('header'); in an action.

Tomáš Fejfar
A: 

If you use

$this->layout()->header = $this->render('index/header.phtml');

It will even use the view, therefore keeping all your variables defined when rendering the header.

I would suggest using something like

<?php echo ($header = $this->layout()->header)? 
      $header : $this->render('headerDefault.phtml'); ?>

in your layout file - it will render a default header from the layout folder if the view script doesn't override it.

gnarf