简体   繁体   中英

Zend Layout render some parts on every page

I'm new to the Zend Framework and I'm trying to get how they want to render things. I'm creating a website and I have a menu which is dynamically created from the database. If I call the correct action I see the menu.phtml view correctly.

Now I'd like for this menu.phtml to be rendered on every page and I have no clue of how to do this. I read about placeholders, helpers, ... but don't seem to get it.

I suppose I have to call the action to generate the menu.phtml, render it and put it in a placeholder I can call from my layout.phtml but I don't see how I do this.

Thanks in advance.

SOLUTION:

In the layout.phtml I added the following line:

<?php echo $this->action('menu',
                            'page',
                            null,
                            array());?>

This will access the menuAction in the PageController and run it. The variables will be filled in, the needed code will be executed before rendering the menu.phtml view script right there.

You can do something like this:

Suppose you are showing the menu in some div called 'header'.

<div id='header'><?= $this->render('menu.phtml'); ?></div>

You have to add correct path for accessing menu.phtml in your working controller.

public function init() {

        $this->view->addScriptPath(APPLICATION_PATH . '/views/scripts/');
}

If you want to pass values to the menu.phtml file.

<?php echo $this->partial('page/menu.phtml', array('var1'=>'value1','var2'=>'value2') ); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM