views:

31

answers:

1

HI all,

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 the 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.

Any ideas? Thanks

+1  A: 

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));
    }
}
Benjamin Cremer
Indeed that's what they are for however this doesn't address my question which isn't about how sending data to a view, but how preparing data ONCE for several views, regardless of the controller/action executed without having to (if possible) modify aforementioned controllers.
extended my answer a bit. some_expensive_data_getter() should be only callen once.
Benjamin Cremer
@user359650 You may still use `foobar($data)` to pass the data from the view.
takeshin

related questions