在ZF2中直接将PhpRenderer与子视图一起使用 [英] Using PhpRenderer directly with child views in ZF2

查看:131
本文介绍了在ZF2中直接将PhpRenderer与子视图一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Zend Framework 2中发送电子邮件时,我想使用视图脚本模板,以便可以利用各种电子邮件模板的布局.我的想法是,我拥有一个具有共享标记并在某处回显内容的布局.布局实际上是如何正常工作的.为此,我尝试将子视图模型添加到布局视图模型中,并使用PhpRenderer进行渲染.

When sending out e-mails in Zend Framework 2, I want to use view script templates such that I can leverage a layout for my various e-mail templates. The idea is that I have a layout which has the shared markup and echoes out content somewhere. Exactly how layouts work in general. To accomplish this, I am trying to add a child view model to my layout view model and render it with the PhpRenderer.

public function someMethod() {
    $child = new ViewModel();
    $child->setTemplate('test-template');

    $layout = new ViewModel();
    $layout->setTemplate('email-layout');
    $layout->addChild($child, 'content');

    $phpRenderer = new \Zend\View\Renderer\PhpRenderer();
    $phpRenderer->setCanRenderTrees(true);

    $resolver = new \Zend\View\Resolver\TemplateMapResolver();
    $resolver->setMap($this->serviceLocator->get('ViewTemplateMapResolver'));

    $phpRenderer->setResolver($resolver);
    $output = $phpRenderer->render($layout);
}

布局视图脚本:

Hello from the e-mail layout script!

<?php echo $this->content; ?>

发生的情况是,只有布局的内容存储在$output中,而布局视图脚本中的$this->contentNULL.

What happens is that only the layout's content is stored in $output and $this->content within the layout view script is NULL.

我想做的是将HTML存储在字符串中,这样我就可以通过电子邮件发送它.我实例化了一个子视图模型,该模型代表了我想在布局中渲染的视图脚本.然后,我实例化布局视图模型并添加子视图模型.然后,我创建一个PhpRenderer,并向其中添加TemplateMapResolver,以便它将使用模板映射来解析模板.最后,我在PhpRenderer上调用render方法,该方法将ViewModel作为其第一个参数.我已经将两个模板都添加到了template_map配置中,并且使用正确的名称和正确的路径正确设置了模板映射.

What I am trying to do is to store the HTML in a string such that I can send it in an e-mail. I instantiate a child view model, which represents the view script that I would like to render within the layout. Then I instantiate the layout view model and add the child view model. Then I create a PhpRenderer and add a TemplateMapResolver to it such that it will resolve templates by using the template map. Lastly, I call the render method on the PhpRenderer which takes a ViewModel as its first parameter. I have added both templates to my template_map configuration and the template map is set correctly with the correct names and correct paths.

问题是我在其文档嵌套视图模型,除了不从控制器操作中返回视图模型外,我在做同样的事情.如果我从控制器操作中返回$layout,它将起作用.

The thing is that I do not see any handling of child views in its render method. This makes me think if I am using the PhpRenderer class incorrectly and perhaps that is why it doesn't work. I have checked the documentation for nesting view models, and I am doing the same thing, except not returning the view model from a controller action. If I return $layout from a controller action, it works.

如果我查看Zend\View\View render 方法,我可以看到它呈现了子视图.好像我不能像我想要的那样使用PhpRenderer.但是我可以使用Zend\View\View吗? render方法不返回任何内容,但会触发一些事件.我对事件管理器还不太熟悉,因此我不确定如果使用此方法,如何/是否可以捕获输出.监听响应事件?我必须检查一下我猜得出的视图(或在完成工作后分离我的侦听器).我不知道那有什么好处.

If I look at Zend\View\View's render method, I can see that it renders child views. It would seem as if I cannot use the PhpRenderer like I want to. But can I use the Zend\View\View for this? The render method does not return anything, but triggers some events. I am not too familiar with the event manager yet, so I am not sure how/if I can capture the output if using this method. Would it be sensible to listen to the response event? I would have to check which view was rendered I guess (or detach my listener after doing my work). I don't know if that's any good.

任何想法都值得欢迎.谢谢!

Any ideas are more than welcome. Thank you!

推荐答案

$this->content为空,因为PhpRenderer只是渲染自身级别,不包括子视图模型.

$this->content is null because PhpRenderer just render self level not including children view models.

实际上,ZF2 MVC渲染使用的是Zend \ View \ View-> render(),而不使用Zend \ View \ Renderer \ PhpRenderer-> render(),因此您需要完全像Zend \ View \ View一样渲染树模板.确实:

Actually the ZF2 MVC rendering are using Zend\View\View->render() but not Zend\View\Renderer\PhpRenderer->render(), so you need to render tree templates exactly like what Zend\View\View does:

foreach ($layout as $child) {
    if ($child->terminate()) {
        continue;
    }
    $child->setOption('has_parent', true);
    $result  = $view->render($child);
    $child->setOption('has_parent', null);
    $capture = $child->captureTo();
    if (!empty($capture)) {
        if ($child->isAppend()) {
            $oldResult=$model->{$capture};
            $layout->setVariable($capture, $oldResult . $result);
        } else {
            $layout->setVariable($capture, $result);
        }
    }
}

$output = $view->render($layout);

请参阅ZF2源代码以了解更多信息:

See ZF2 source code to know more:

https://github.com/zendframework/zf2/blob/master/library/Zend/View/View.php#L222

这篇关于在ZF2中直接将PhpRenderer与子视图一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆