Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a variable from Controller to a Partial in Zend

For some reason I cant fid the right syntax to pass a variable from my controller (profileController) to a partial (video.phtml).

I know you pass variables to views like this

$this->view->somedata = "somedata"; 

and you pull it in the view with

< ?= $this->somedata ?>

how would you do so for a partial ?

like image 578
TuK Avatar asked Nov 19 '10 11:11

TuK


1 Answers

When you render the partial (probably from the view, right) <?php echo $this->partial(scriptpath, [module], [data])?>, you can also pass in an module where the script is located, and data that the partial should have access to. This is the reason for partials, to only allow it to see certain data.

So, say you have the following scenario: You push a lot of data from the controller into the view. In a certain partial, that will be re-used in multiple places in your app, you want to ensure that it only has access to a certain type of data, and you want the data to be named consistently whenever it is rendered. You would then do something like:

In the controller:

$this->view->namedVariableThatCouldBeWhatever = $data;

In the view:

echo $this->partial(
  $partialName, 
  array (
    'standardName' => $this->namedVariableThatCouldBeWhatever
  )
);

In the partial:

<?php foreach($this->standardName as $item) : ?>
   //render partial
<?php endforeach; ?>
like image 78
PatrikAkerstrand Avatar answered Oct 06 '22 00:10

PatrikAkerstrand