dbq
dbq

Reputation: 111

Zend, MULTIPLE layouts with COMMON base code

I know how to use multiple layouts (per Controllers and per Modules).

I have 5 diffirent layouts and they switch fine every time I call diffirent module. But the problem is I see they all have SOME portion of common code (like doctype, stylesheets etc.)

Will you tell me if Zend allows using multiple layouts but setting the common root for them? Now if i want to add stylesheet I need to modify 5 diffirent layouts what I belive could be done in a better manner.

Thanks in advance.

Upvotes: 2

Views: 406

Answers (2)

ForrestLyman
ForrestLyman

Reputation: 1652

I usually create a wrapper layout that renders the content with sublayouts.

  1. create a new folder in layouts called sublayouts
  2. create as many sublayouts as you need with one default so you don't have to set it if you don't need to.

    /application /layouts wrapper.phtml /sublayouts default.phtml

In wrapper.phtml:

$sublayout = $this->layout()->sublayout ? $this->layout()->sublayout : 'default';
echo $this->render('sublayouts/' . $sublayout . '.phtml');

In sublayouts/default.phtml (add anything):

<?= $this->layout()->content ?>

Upvotes: 1

Richard Parnaby-King
Richard Parnaby-King

Reputation: 14862

There are view helpers that are used for these situations. headScript and headLink.

In your layouts all you need to do is

echo $this->headScript(), $this->headStyle();

Then, in your bootstrap add:

public function _initScriptsAndStyles()
{
  //get the view object
  $this->bootstrap('view');
  $view = $this->getResource('view');

  //add javascript
  $view->headScript()
       ->appendFile('/js/your-js.js')
       ->appendFile('/js/another-js.js');

  //add css files
  $view->headLink()
       ->appendStylesheet('/styles/basic.css');
}

Upvotes: 0

Related Questions