Dynamic views in ZF2

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Dynamic views in ZF2

henit
This post was updated on .
Hello. My employer has decided to go for ZF2 after developing our own software platform from bottom the last 15 years. My task is to test it in the beta period and find out how to use the framework. I am new to ZF, but has a lot of experience from other frameworks and PHP systems.

At this point, I am testing different ways of using the MVC module system in our customer solutions. One of the issues there is how to make more dynamic layouts. I want to be able to use the same layouts but with different content based on different criteria. This might come from database (CMS settings etc), or it might be based on what happens in the controller.

So for instance, if I have this simplified layout view:

<html>
<body>
<div class="left">
</div>
<div class="main">
<?php print($this->content); ?>
</div>
</body>
</html>

then a ViewModel class returned from the controller action could be the source of what is displayed in the "main" block. But then if i want to reuse this layout for many different pages, how can I best do that with a flexible way of including content in the "left" (and other similar) block? I have tried:

1. Putting a print($this->renderChildModel('left')); in the left block. I can then add a child model to the returned ViewModel in the controller, but then I have to add the child models in every controller action method.
2. If i run $this->render('something'); then another view (like a menu in the left block) can be rendered directly. This could work fine, but I might not always want to render the same content on different pages on the website.

If someone has a good example of how to do this, or can point me in the right direction, I would really appreciate it!
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic views

HHGAG
This post was updated on .
Hi,

we had the same issue avoiding/reducing template changes from customers, so we've added placeholders like header, asideL, article, asideR, footer, etc
which are childmodels assigned to the layout. These placeholder models store their content in a stack/priority queue which will be iterated in their own templates. So you can just assign your content to one of the placeholder and let it automatically render to the selected place.
Hasan H. Gürsoy (HHGAG)
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic views

henit
Thanks for the quick reply.

I tried something like this first:

function landingAction() {
    $model = new ViewModel();
    $model->setTemplate('application/index/landing');
   
    $left = new ViewModel();
    $left->setTemplate('application/test/left');
    $this->layout()->addChild($left, 'left');
   
    $this->layout('layout/layout1');
   
    return $model;
}

and a couple of other childModels aswell, and then rendering them in the layout view file like this: print($this->renderChildModel('left'));

But this adds a lot of extra code in every controller action. Do you have any example of how you use placeholders?
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic views

HHGAG
This post was updated on .
onBootstrap:

        public function set(ModelInterface $model, $captureTo = 'container')
        {
                $placeholder = new SmartyModel(array());
                $placeholder->setTemplate('placeholder/' . $captureTo);
                $model->addChild($placeholder, $captureTo);
                return $model;
        }

which will be used 5 times like:

        $model = $e->getParam('application')->getMvcEvent()->getViewModel();

        $model = Placeholder::set($model, 'header');
        $model = Placeholder::set($model, 'nav');
        $model = Placeholder::set($model, 'article');
        $model = Placeholder::set($model, 'asideL');
        $model = Placeholder::set($model, 'asideR');
        Placeholder::set($model, 'footer');

Template looks like that (Smarty Template):

{foreach $__vars as $module}{$module}{/foreach}

contents will be assigned to $__vars which is iterated in the template

We use it at the moment for widgets/listeners:

        $iterator = $e->getViewModel()->getIterator();
        while ($iterator->valid()) {
                if($iterator->current()->captureTo() == $this->position) {
                        $iterator->current()->addChild($model, $iterator->current()->captureTo() . '_' . get_class($this));
                }
                $iterator->next();
        }

but this way will be optimized, when Matthew and colleagues have finished their work :)
Hasan H. Gürsoy (HHGAG)
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic views

henit
This post has NOT been accepted by the mailing list yet.
That worked. Thanks!