聽起來像你需要一個ViewHelper
http://framework.zend.com/manual/en/zend.view.helpers.html。創建一個自定義幫助程序,它將從模型中獲取數據並僅輸出它。這種方式不依賴於任何控制器,可以在佈局或任何視圖腳本中調用。例如:
// views/helpers/Widget.php
class Zend_View_Helper_Widget extends Zend_View_Helper_Abstract
{
protected $_model = null;
protected $_view = null;
public function widget()
{
$data = $this->_getDataFromModel();
return $this->_view->partial('widget.phtml', array('data' => $data));
}
public function setView(Zend_View_Interface $view)
{
if($this->_view === null) {
$this->_view = $view;
}
return $this->_view;
}
protected function _getDataFromModel()
{
$this->_model = $this->_getModel();
return $this->_model->getDataForWidget();
}
protected function _getModel()
{
if($this->_model === null) {
$this->_model = new Model_Widget(); // or whatever it's called
}
return $this->_model;
}
的部分腳本:
// views/scripts/widget.phtml
<div class="widget-class"><?php echo $this->data; ?></div>
而且當你需要它在你的看法只是把它像<?php echo $this->widget(); ?>
請注意,我渲染部件在一個單獨的局部視圖腳本,只是爲了避免在幫助程序中存在html/css。
希望這有助於讓你開始:)