简体   繁体   English

Zend框架 - 从视图访问View Helper

[英]Zend Framework - Access to View Helper from View

I have a problem with accessing a custom view helper in a view. 我在访问视图中的自定义视图助手时遇到问题。 Call the helper in the layout.phtml works perfectly. 在layout.phtml中调用助手可以很好地工作。

ZF Version: 1.11.2 ZF版本:1.11.2

application.ini 的application.ini

resources.view[] =

Bootstrap: 引导:

protected function _initViewSettings()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->doctype('XHTML1_STRICT');

    $view->setScriptPath(APPLICATION_PATH . '/views/scripts/');
    $view->addHelperPath(APPLICATION_PATH . '/layouts/helpers/', 'Layout_Helper_');
    $view->addHelperPath(APPLICATION_PATH . '/views/helpers/', 'View_Helper_');

}

Helper: (application/views/helpers/Loader.php) 助手:(应用程序/ views / helpers / Loader.php)

class View_Helper_Loader extends Zend_View_Helper_Abstract
{
    public function loader()
    {
         $loader_html = '
             <div class="loading">
             <div class="bullet"></div>
             <div class="bullet"></div>
             <div class="bullet"></div>
             <div class="bullet"></div>
         </div>';

        return $loader_html;
    }
}

View: 视图:

<?php
    echo $this->loader();
?>

Error: 错误:

Plugin by name 'Loader' was not found in the registry; used paths: Zend_View_Helper_: Zend/View/Helper/

Why can't I use the helper in my view? 为什么我不能在视图中使用帮助器?

Perhaps try explicitly adding your view to the view renderer: 也许尝试将您的视图显式添加到视图渲染器:

    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
        'ViewRenderer'
    );
    $viewRenderer->setView($view);

I can't quite recreate your problem - having it work in the layout but not the view. 我不能完全重现你的问题 - 让它在布局中工作但不在视图中工作。

If I add a view resource to the application.ini, some of the wiring happens automagically for me in ZF 1.11.14, but if I create the view (new Zend_View) in the bootstrap as a resource I have to explicitly pass it to the view renderer. 如果我向application.ini添加一个视图资源,在ZF 1.11.14中我会自动发生一些连接,但是如果我在引导程序中创建视图(新的Zend_View)作为资源我必须明确地将它传递给查看渲染器。

Fixed it. 固定它。

The problem were the following lines in my action: 我的行动中存在以下问题:

    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

I thought the result of my helper would be parsed into my view anyway. 无论如何,我认为我的助手的结果将被解析到我的视图中。 Bad mistake. 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM