Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework - layout best practices for 'includes'

I'm pretty new to Zend.. I'm just wondering how to model my site's layout/structure. My site will have an user profile section, admin section, and the generic the default view of the site.

For the admin and profile, I'll have custom elements in the headers and footers, otherwise I want to default to a generic header/footer.

I want the ability to have an entirely custom skin separate from the default view, how should I structure things?

So far I have have created a directory in application/ named layouts. I have modified the application.ini file so it accounts for that:

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

So my default layout view now exists in application/layouts/scripts/layout.phtml, I have the header and footer embedded in this layout.phtml file but I would like to strip them out and have them in separate files.

Could anyone assist me in coming up with the structure for this? So to re-iterate I want a custom default layout, custom admin layout, custom user profile layout, and for all 3 layouts I want customizable heading/footer "includes" but I'm not sure how this is done properly in Zend as I'm accustomed to include files.

like image 335
meder omuraliev Avatar asked Dec 30 '22 10:12

meder omuraliev


2 Answers

Directory structure:

/application
  /layouts
    /scripts
      /layout.phtml
      /profile.phtml
      /admin.phtml
      /_header.phtml
      /_footer.phtml

layout.phtml:

<?php echo $this->doctype('HTML4_STRICT') ?>
<html> 
<head>  
  <title>Bah</title>
</head> 
<body>
  <?php echo $this->render('_header.phtml') ?>
  <?php echo $this->layout()->content ?>
  <?php echo $this->render('_footer.phtml') ?>
</body>
</html>

profile.phtml:

<?php echo $this->doctype('HTML4_STRICT') ?>
<html> 
<head>  
  <title>Profile</title>
</head> 
<body>
  <!-- profile header -->
  <?php echo $this->layout()->content ?>
  <!-- profile footer-->
</body>
</html>

FooController.php:

profileAction()
{
    // do stuff
    $this->_helper->layout->setLayout('profile');
}

This method allows you to change the entire page structure of different layouts (The admin suddenly needs a sidebar!). You trade off some code duplication for enhanced flexibility and maintainability.

If this is not important to you, a view helper would work also (query the Front Controller to find out if the request came from either the admin or profile actions, switch). For my tastes though, that type of logic is too involved to belong in the view.

like image 194
Richard Nguyen Avatar answered Jan 10 '23 08:01

Richard Nguyen


If the view helpers are complex use concrete view helper. If they are simple like <h1>my title</h1> use partial view helper to render a template.

like image 26
Tomáš Fejfar Avatar answered Jan 10 '23 10:01

Tomáš Fejfar