[Solar-talk] helper stack

Paul M Jones pmjones at solarphp.com
Mon Aug 28 10:00:20 PDT 2006


On Aug 28, 2006, at 1:55 AM, Rodrigo Moraes wrote:

> On 8/25/06, Paul M Jones wrote:
>> You want to add it to the view object the controller uses
>> automatically, not create your own view.  The way to do this is to
>> override _getView() in your app.
>>
>> public function _getView()
>> {
>>         // build a $stack, and then...
>>         $view = parent::_getView();
>>         $view->addHelperClass($stack);
>>         return $view;
>> }
>
> I'm starting to use dynamic layout paths and have been trying to do
> the same for views/layouts. The only way I had success adding a
> different path for a layout was to add it directly in the layout:
>
> <body>
> <?php
> $this->addTemplatePath($this->layout_path);
> include $this->template('layout.php');
> ?>
> </body>
>
> I would prefer to manipulate the template path outside of the layout,
> of course. But I'm very confused about how to do this. :-|

The method _setViewLayout() rides to the rescue!  ;-)  In your app,  
override it like this:

     protected function _setViewLayout($view)
     {
         // change the view object over to layouts
         $view = parent::_setViewLayout($view);

         // now add our own stuff
         $view->addTemplatePath($this->layout_path);

         // done!
         return $view;
     }

Note that the _setViewLayout() method is the only "hook" that takes a  
parameter; i.e., the $view object that was used to run the view  
script).  That $view object is used twice: first to run the view  
script, then it is pointed to the layout paths and re-used.  That way  
all the variables and helpers used by $view stay in place for the  
layout use use.

--

Paul M. Jones  <http://paul-m-jones.com>

Solar: Simple Object Library and Application Repository
for PHP5.   <http://solarphp.com>

Savant: The simple, elegant, and powerful solution for
templates in PHP.   <http://phpsavant.com>




More information about the solar-talk mailing list