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 に答える