Question

On most project I use multiple layout scripts.

Sometimes I need some data in my layouts that are layout-specific, instead of page-specific. Which layout gets used though, IS page-specific.

So the bootstrap or actioncontroller would be good places to select a layout. But IMHO they would not be good places to inject the data a particular layout expects.

The only other place I can think of is to just write some business logic in the layout viewscript itself. Though that's something I'd rather not do either :)

Where do you write your layout-specific business logic?

-- UPDATE:

layout-specific business logic could be;

  • username of currently logged-in user
  • amount of new messages in user's inbox
  • random "did you know..?" tip
Was it helpful?

Solution

Stuff like this is best done from a ViewHelper

class ViewHelper_RandomTip
{
    public function randomTip()
    {
         $tip = TipsModel::getRandom();
         return "<div><h1>Random Tip</h1><p>$tip</p></div>");
    }
    // ...
}

Then in your layout, use it with

echo $this->randomTip();

Note that this is example code not intended to run anywhere. Exactly how you access your model from the Helper and how you return the content is completely up to you. You will also have to find a mean to register the ViewHelpers with the Layout. And there will be people telling you, you may not access the model from the View (which is wrong)

Please also see these related questions:

And have a look at how Zend Framework does this kind of work for further information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top