I don't think this part fits in controllers at all.
You should really do it in a listener during the `render` event (keep
rendering stuff near all the rest of the rendering stuff).

The InjectTemplateListener is just an example of how to manipulate things,
but in such a listener you could actually add any variables you'd need to
render your header/footer (along with the header and footer viewmodels too
if you want).

You can manipulate a ViewModel by accessing it during the `render` event as
in
https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php#L110-131




Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com



On 3 September 2012 12:38, Ralf Eggert <r.egg...@travello.de> wrote:

> Hi,
>
> I am still struggling about the best ZF2 way to render a footer and a
> sidebar within a layout.
>
> I found this example within Robs Test App:
>
> ------------------------------------------------------------------------
>     public function addAnotherViewModelToLayoutAction()
>     {
>         // Use an alternative layout
>         $layoutViewModel = $this->layout();
>         $layoutViewModel->setTemplate('layout/another');
>
>         // add an additional layout to the root view model (layout)
>         $sidebar = new ViewModel();
>         $sidebar->setTemplate('layout/footer_one');
>         $layoutViewModel->addChild($sidebar, 'footer');
>
>         return new ViewModel();
>     }
> ------------------------------------------------------------------------
>
> Looks easy at first glance. But I don't want to repeat this code in
> every action method I have. This should be done application wide.
> Besides the rendering of such a footer or sidebar template I also want
> to access my service class to grab some data and pass it to footer and
> sidebar for output.
>
> Should I write my own listener for the 'render' event?
>
> Should I write a controller plugin that is processed automatically?
>
> Or should I write a view helper that is able to access my service class
> for the data?
>
> Or is there another solution, maybe the best practice solution?
>
> Or should I use the InjectTemplateListener which Marco suggested to me a
> couple of weeks ago? If yes, has someone an example for it?
>
> https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/View/Http/InjectTemplateListener.php
>
> Thanks for your ideas?
>
> Best regards,
>
> Ralf
>
> --
> List: fw-general@lists.zend.com
> Info: http://framework.zend.com/archives
> Unsubscribe: fw-general-unsubscr...@lists.zend.com
>
>
>

Reply via email to