开发者

best practice on rendering several views based on same dynamic data

开发者 https://www.devze.com 2023-01-09 19:43 出处:网络
I would like to have some input on the following: I would like some views to be systematically rendered (I can call $this->render from

I would like to have some input on the following:

  1. I would like some views to be systematically rendered (I can call $this->render from my layout for these views) regardless of which controller/action is executed.
  2. However the content used in these views is based on the same dynamically generated data and the code behind it is quite complex so I can't put the logic inside the views for obvious optimization/performance issues.
  3. I could use $this->_helper->actionStack in each controller to call another controller in which data for the views would be prepared however I would like to do without modifying t开发者_Python百科he existing controllers

I would be tempted to put something in the bootstrap since what I want is common to my application however I just don't know what to do.


That's what View Helpers are for.
In the View Helper you can fetch your data (through models or service layer) and prepare it for output.

<?php
class View_Helper_Foobar extends Zend_View_Helper_Abstract
{
    protected $_data;

    public function foobar()
    {
        if (null !== $this->_data) {
            $this->_data = some_expensive_data_getter();
        }

        return $this;
    }

    public function __toString()
    {
        return $this->view->partial('foobar.phtml', array('data' => $this->_data));
    }
}
0

精彩评论

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

关注公众号