Symfony2分支代码在xml模板中不起作用,因为它在html中工作 [英] Symfony2 twig code doesn't work in xml template as it works in html

查看:90
本文介绍了Symfony2分支代码在xml模板中不起作用,因为它在html中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为什么分支代码 {%set code = code(_self)%} 在xml模板中不起作用。

 <! -  src / Acme / DemoBundle / Resources / views / Demo / hello.xml.twig  - > 
< hello>
< name> {{name}}< / name>
< / hello>
{%set code = code(_self)%}



  {%extendsAcmeDemoBundle :: layout.html.twig%} 

{%块名称Hello〜name%}

{%block content%}

Hello {{name}}!< / h1>
{%endblock%}

{%set code = code(_self)%}


解决方案

这不是一个原生的Twig扩展,它是由Symfony2标准版的 DemoBundle 提供的附加扩展( https:// raw。 github.com/symfony/symfony-standard/master/src/Acme/DemoBundle/Twig/Extension/DemoExtension.php )。我刚刚测试过它的工作原理,但是这个 code() Twig扩展只用于HTML模板而不是XML模板。如果您显示XML模板的源代码,您将看到控制器的输出,但不会显示,因为生成的XML变得无效。如果你真的想要输出,你可以使用这个:

 <?xml version =1.0encoding =UTF- 8\" >?; 
< code> {{code(_self)| escape}}< / code>


why twig code {% set code = code(_self) %} doesn't work in xml template.

<!-- src/Acme/DemoBundle/Resources/views/Demo/hello.xml.twig -->
<hello>
    <name>{{ name }}</name>
</hello>
{% set code = code(_self) %}

like it works in html template.

{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block title "Hello " ~ name %}

{% block content %}
    <h1>Hello {{ name }}!</h1>
{% endblock %}

{% set code = code(_self) %}

解决方案

Well it's not a native Twig extension it's an additional extension provided by the DemoBundle of the Symfony2 standard edition (https://raw.github.com/symfony/symfony-standard/master/src/Acme/DemoBundle/Twig/Extension/DemoExtension.php). I've just tested and it works but this code() Twig extension is only intended to be used in an HTML template not an XML one. If you show the source of you XML template you will see the output of your controller but it will not show up as the generated XML becomes invalid. If you really want the output, you can use this:

<?xml version="1.0" encoding="UTF-8"?>
<code>{{ code(_self) | escape }}</code>

这篇关于Symfony2分支代码在xml模板中不起作用,因为它在html中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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