Zend_View Helper视图助手使用说明

Bootstrap.php:

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();




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值