Another method I use:
I extend the Zend_Controller_Action and put an Intermediate class there,
as you can see from this sample controller.
The inter class contains a toView method, I can then simply assign any
values.
I control all elements in my translation files so product can change
anything through POEdit works really well.
Please comment, would like to know from people within this thread what
is the best method ie speed wise with least overhead.
class Finances_InvoicesController extends Zend_Action
{
// {{{ properties
protected $_view = null;
// }}}
// {{{ __init()
public function init()
{
// Maps to arg 'view' from: $frontController->setParam('view',
$view);
$this->_view = $this->getInvokeArg('view');
}
// }}}
// {{{ indexAction()
/**
* Basically this action acts as the home page.
*
* @access public
* @return object
*/
public function indexAction()
{
// Add items required from the registry
$translate = Zend_Registry::get('translate');
// Add items required from the registry
$config = Zend_Registry::get('config');
// Sort out the SMS Navigation
$finances = new Modules_Finances_Classes_Finances();
if (empty($this->getParam['selfbill'])) {
$table = 'db_tbl_finances_invoices';
$addon = 'invoices';
} else {
$table = 'db_tbl_finances_self_bills';
$addon = 'selfbill';
}
// Add items required from the registry
$db = Zend_Registry::get('DB');
$string = '';
for ($i = 1; $i <= 15; $i++) {
$string .= 'IF (a.qty' . $i . ', a.qty' . $i . ' * a.unit'
. $i . ', 0) + ';
}
$string = rtrim($string, ' + ');
// Query to get the invoices.
$select = $db->select()
->from(array('a' => $config->$table), array('a.code',
'b.company',
'a.sale_date', 'a.paid_date', 'total_net' => '@tn:=' .
$string,
'output_vat' => '@ov:=FORMAT(IF (a.vat_exempt = "0",
(@tn) * 1.175 - @tn, 0), 2)',
'total_gross' => 'FORMAT((@tn) + (@ov), 2)', 'a.status'))
->joinLeft(array('b' => $config->db_tbl_finances_debtors),
'a.debtor_id = b.debtor_id', array())
->order('a.code DESC');
$result = $db->fetchAll($select);
if ($result) {
// Create the options
$options['dataGrid'] = array('perPage' => '50', 'page' =>
$this->_getParam('page'));
$options['fields'] = array(
'code' => 'Code', 'company' => 'Company', 'sale_date' =>
'Sale Date',
'paid_date' => 'Paid Date', 'total_net' => 'Total Net',
'output_vat' => 'Output VAT',
'total_gross' => 'Total Gross', 'status' => 'Status'
);
$options['primary_key'] = array(
'code'
);
// Render the table
$rowSet = parent::tableRender($result, 'HTMLTable', $options);
}
if (is_null($this->_getParam('ajax'))) {
// Render the view
$this->_view->page_title =
$translate->_('financesInvoices_page_title');
$this->_view->h1 =
$translate->_('financesInvoices_h1');
$this->_view->title =
$translate->_('financesInvoices_title');
$this->_view->navigation =
$translate->_('financesInvoices_navigation');
$this->_view->feature = $finances->getHeader();
$this->_view->text =
$translate->_('financesInvoices_text') . $rowSet['string'];
$this->_view->template =
$translate->_('financesInvoices_template');
$this->_view->template_span =
$translate->_('financesInvoices_template_span');
// Render the view
parent::toView($this->_view);
} else {
echo $rowSet['string'];
}
}
// }}}
// {{{ __call()
/**
* Method to redirect the page if a dodgy action is put into this
controller
*
* @param string $action
* @param string $arguments
* @return string
*/
public function __call($action, $arguments)
{
$this->_redirect('/finances');
}
// }}}
}
Arnaud Limbourg wrote:
Matthew Weier O'Phinney wrote:
I throw a Zend_View object in the registry, and then access this from my
controllers and plugins. The benefit of doing this is that the
controllers can set values in the view that are unused in their
individual view, but used later in the sitewide template.
Then, I use a dispatchLoopShutdown() plugin to inject any generated
content into a sitwide template:
class SiteTemplatePlugin extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopShutdown()
{
$response =
Zend_Controller_Front:;getInstance()->getResponse();
$view = Zend_Registry::get('view');
$view->content = $response->getBody();
$response->setBody($view->render('site.phtml'));
}
}
Which poses a problem when you want to send back json (or whatever) and
you don't want a site wide template :)
Arnaud.