4

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.

hhoud
  • 518
  • 2
  • 6
  • 18

1 Answers1

4

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') ); ?>
Muhammad Zeeshan
  • 8,722
  • 10
  • 45
  • 55
  • The problem with this is that I can't access menu.phtml because it's in the views/scripts folder and the layout is in the layouts/scripts/ folder. I tried using ../../ to get to it but I get an error saying: "Requested scripts may not include parent directory traversal " – hhoud Jun 28 '11 at 12:29
  • Ok, so now the error is already gone but nothing shows up. This is what I do: in a controller called AlbumController.php I have added the addScriptPath line. In the layout.phtml I added the render('menu.phtml'). When I go to mywebsite/Album it shows me the album page but the menu.phtml does not show. – hhoud Jun 28 '11 at 13:01
  • can you show the layout.phtml code in question? Also please verify your path according to your directory structure. – Muhammad Zeeshan Jun 28 '11 at 13:05
  • This is the layout.phtml doctype() ?> Test headLink()->appendStylesheet('/css/global.css') ?>
    layout()->content; ?>
    `
    – hhoud Jun 28 '11 at 13:08
  • use `render('page/menu.phtml'); ?>` – Muhammad Zeeshan Jun 28 '11 at 13:14
  • Ok that was a stupid mistake of mine, sorry for that. Now it shows the menu.phtml. Only problem I have is that the variables accessed in menu.phtml are empty (eg. $this->menu;), I suppose that is because the MenuController hasn't been processed. Know a solution for that? – hhoud Jun 28 '11 at 13:22
  • @hh354: Next time please just edit and update your question with additional code, instead of pasting it in a comment ;) – Phliplip Jun 28 '11 at 13:36
  • I know about the partial method but the problem is that the values are coming from a database. Can't call a database from the layout.phtml. This would all be so much easier if you could call multiple urls of controllers and just let it render the views together in one page. So how do I get to the code I have in the MenuController and run it to get the values from the database into my menu.phtml? – hhoud Jun 28 '11 at 13:38
  • If the solution helped you in showing the menu you can accept the answer :) – Muhammad Zeeshan Jun 28 '11 at 13:41
  • Well, I accepted the answer because the view is showed (eg. the plain text in the view), but the contents of the view don't show if they are variables, so I'd like a solution for that too. But I already accepted your answer for the first part ;) – hhoud Jun 28 '11 at 13:44
  • I think you have to make a function to get this work. and need to call this function in your controller in order to pass the values to menu.phtml everytime. – Muhammad Zeeshan Jun 28 '11 at 13:48
  • see the reference link http://stackoverflow.com/questions/746260/where-do-i-save-partial-views-in-zend-framework-to-be-accessible-for-all-views – Muhammad Zeeshan Jun 28 '11 at 14:15
  • Thanks for the reference link, I found the solution! Seems like my reasoning was correct but I just couldn't find the solution I needed. The actionHelper solved my problem as it allows me to call the menuController and let it render the menu, so in the end you don't need the partial() nor the render(). – hhoud Jun 28 '11 at 14:34
  • can you share the solution so that everyone can access this. – Muhammad Zeeshan Jun 28 '11 at 14:38