Frank M.
Frank M.

Reputation: 171

How to retrieve parameters in a Zend layout

I'm working through the book "Zend Framework - A beginners guide". Part of the third chapter describes working with a masterlayout.

For my navigation I'd like to set the id-attrib of the body dynamically. How can I get a parameter from any controller to this layout-file?

The master-layout is set in application.ini:

resources.layout.layoutPath = APPLICATION_PATH "/layouts"
resources.layout.layout = master

greetings Frank

Upvotes: 1

Views: 1984

Answers (2)

OV Web Solutions
OV Web Solutions

Reputation: 825

The best way to do this, is to use placeholders. Here's an example layout:

master.phtml
------------
<html>
   <head>
      <title>My Master Layout</title>
   </head>
   <body id="<?= $this->placeholder('my_dynamic_id_attrib'); ?>">
    ...
   </body>
</html>

Note that the value for the "id" attribute starts with "<?=". This is the same as "<?php echo" and it should work correctly if you're using the default .htaccess file which Zend recommends. If "<?=" does not work for you, simply replace it with:

<body id="<?php echo $this->placeholder('my_dynamic_id_attrib'); ?>">

Now, in your controller, you can set your dynamic id using:

IndexController.php
-------------------
public function indexAction(){

  //------------------------------------
  // Can either be $_GET or $_POST, etc.
  $dynamicParam = $this->_getParam('id');

  //------------------------------------
  // Set the dynamic id
  $this->view->placeholder('my_dynamic_id_attrib')->set($dynamicParam);
}

Upvotes: 2

wajiw
wajiw

Reputation: 12269

You can use view vars for simple variables you need to pass into layout scripts:

In your controller:

function indexAction()
{
    $this->view->pageTitle = "Zend Layout Example";
}

In your layout script:

<html>
<head>
    <title><?php echo $this->escape($this->pageTitle); ?></title>
</head>
<body></body>
</html>

Upvotes: 2

Related Questions