meder omuraliev
meder omuraliev

Reputation: 186662

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.

Upvotes: 4

Views: 4950

Answers (4)

piddl0r
piddl0r

Reputation: 2449

To have a different layout/config for various sections of a site, I use Modules with the following module setup. Module Config

[Edit]

Directory Structure :

/application
    /forms
    /models
    /modules
        /admin
            /config
            /contollers
            /layout
            /views
        /default
            /config
            /contollers
            /layout
            /views

Upvotes: 1

Richard Nguyen
Richard Nguyen

Reputation: 1321

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.

Upvotes: 9

Tom&#225;š Fejfar
Tom&#225;š Fejfar

Reputation: 11217

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.

Upvotes: 1

Arthur Frankel
Arthur Frankel

Reputation: 4705

An implementation that I use is as follows:

In each of the layout phtml files (default, admin, etc)

I use:

<?= $this->action('header', 'page', 'default') ?>
<?= $this->layout()->content ?>
<?= $this->action('footer', 'page', 'default') ?>

so in my page controller I may have an action called "headerAction" or "adminHeaderAction" which does nothing (or actually could perform specific tasks) but returns a header.phtml (where I can customize my header information for that layout). I can then customize my header and footer separate from the content.

In my controller I just need to set the appropriate layout:

$this->_helper->layout->setLayout('adminLayout');

Upvotes: -1

Related Questions