Bootstrap.php:
视图助手类:/library/Won/View/Helper/Img.php
模板调用:index.phtml:
protected function _initView()
{
$view = new Zend_View();
$view->doctype('XHTML1_STRICT');
$view->headTitle('My First Zend Framework Application');
$view->addScriptPath(APPLICATION_PATH.'/modules/admin/views/scripts');
$view->addScriptPath(APPLICATION_PATH.'/modules/blog/views/scripts');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
//添加视图助手路径
$view->addHelperPath('../library/Won/View/Helper/', 'Won_View_Helper');
Zend_Registry::set('view', $view);
return $view;
}
视图助手类:/library/Won/View/Helper/Img.php
/**
* View Helper:图片助手
**/
class Won_View_Helper_Img extends Zend_View_Helper_Abstract
{
public function img($src, $width, $height, $alt = '', $options = array())
{
if (empty($alt)) {
// 为了确保每个图片都是 alt,以优化 SEO
throw new Zend_View_Exception('Alt attribute should be filled.');
}
$basepath = $this->view->serverUrl() . '/upload/images/';
$img = '<img src="' . $basepath . $src . '" '="" .="" 'height="' . $height . '" alt="' . $alt . '" width="' . $width . '">';
return $img;
}
public function echosth() {
return 'aaaa';
}
}
模板调用:index.phtml:
//当视图助手类只有一个方法或者调用视图助手类的第一个方法时
echo $this->img('logo.jpg', 100, 100, 'woniu(woniu.me)');
//也可以这样写(视图助手类的调用其他方法)
echo $this->getHelper('Img')->img('logo.jpg', 100, 100, 'woniu(woniu.me)');
echo $this->getHelper('Img')->echosth();