开发者

PHP: Zend_Layout: Where to write business logic?

开发者 https://www.devze.com 2022-12-18 15:25 出处:网络
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.

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 l开发者_C百科ayout 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


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:

  • Can I call a Model from the View
  • What are your templating strategies

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号