0

我需要在触发事件中呈现一个 phtml 文件。

我的代码是:

class Module
{
//...
    public function onBootstrap(MvcEvent $e) {
        $app = $e->getApplication ();
        $sem = $app->getEventManager ()->getSharedManager ();   
        $sem->attach ( 'Events', 'onExampleEvent', function ($e) {
            return 'html...';
        } );
    }
//...
}

我如何html...用渲染的 phtml 文件替换?

4

1 回答 1

2

要呈现您的 phtml 文件,您必须执行以下几个步骤:

  1. 创建一个可以容纳变量容器的 ViewModel 对象。如果您需要一些变量,只需将它们作为数组传递。

    $content = new \Zend\View\Model\ViewModel(array('article' => $article));
    
  2. 指定要与 setTemplate 一起使用的模板。

  3. 使用 ViewRenderer 将 ViewModel 数据与适当的模板合并并返回渲染的 html。

下面编辑的代码将满足您的需求

class Module
{
//...
    public function onBootstrap(MvcEvent $e) {
        $app = $e->getApplication ();

        $sm  = $app->getServiceManager();

        $sem = $app->getEventManager ()->getSharedManager ();   
        $sem->attach ( 'Events', 'onExampleEvent', function ($e) {

            $content = new \Zend\View\Model\ViewModel();
            $content->setTemplate('your/template.phtml');  

            return $sm->get('ViewRenderer')->render($content);                                           

        });
    }
//...
}

文档: http: //framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html

于 2013-08-25T13:31:57.620 回答