Dipak Yadav
Dipak Yadav

Reputation: 114

Symfony2 twig code doesn't work in xml template as it works in html

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

Upvotes: 1

Views: 1144

Answers (1)

COil
COil

Reputation: 7606

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>

Upvotes: 1

Related Questions