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

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

问题描述

为什么树枝代码 {% set code = code(_self) %} 在 xml 模板中不起作用.

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) %}

就像在 html 模板中一样.

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) %}

推荐答案

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

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天全站免登陆