Set variable in the view to be used in the layout

I’m not sure that you should really be doing this, but I recall running into one situation where I wanted to set some variable in the view and have it available in the layout. Here’s how…

In the view:
$this->viewVars[‘myVar’] = ‘something’;

In the layout:
echo $myVar;

Nate pointed out that it can be even easier with just: $this->set(‘myVar’, ‘something’);
As $this->set(); works just as well in the view as in the controller.

Remember about MVC and double check if what you are doing is a correct approach to begin with.

Related Posts