Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep indentation when using blocks in Twig

I try to make a pretty RSS Feed in a Symfony project. For each item, I include one file. It's ok but when I look the output, Twig reset indentation in the block element. Here an example :

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
  <channel>
    <title>Space Raclette</title>
    <description></description>
    <language>fr</language>
    <lastBuildDate>Wed, 30 Nov 2016 11:22:45 +0100</lastBuildDate>

<item>
  <title>Topic de l&#039;ƩtƩ du Capitaine Crochet 2</title>
  <link>...</link>
  <guid isPermaLink="false">.../39fa</guid>
  <description></description>
</item>


<item>
  <title>Topic de l&#039;ƩtƩ du Capitaine Crochet</title>
  <link>...</link>
  <guid isPermaLink="false">.../39fa</guid>
  <description></description>
</item>

  </channel>
</rss>

What can I do to keep the indentation without have bad indentation in the "item file" ? I tried to play with spaceless and -, without success.

Here my files if it can help.

layout.rss.twig :

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>

  <channel>
    <title>{{ channel.brand }}</title>
    <atom:link href="{{ app.request.uri }}" rel="self" type="application/rss+xml" />
    <link>{{ url }}</link>
    <description>{{ channel.description|striptags }}</description>
    <language>{{ channel.lang }}</language>
    <lastBuildDate>{{ last_publication.published|date('D, d M Y H:i:s O') }}</lastBuildDate>

    {% block content %}{% endblock %}

  </channel>
</rss>

index.rss.twig

{% extends 'RSSBundle::layout.rss.twig' %}
{% block content %}
  {% for publication in web_publications %}
    {{ include('RSSBundle:Publication:_single.rss.twig') }}
  {% endfor %}
{% endblock %}

_single.rss.twig

<item>
  <title>{{ publication.title }}</title>
  <link>{{ url }}</link>
  <description></description>
  <pubDate>{{ publication.published|date('D, d M Y H:i:s O') }}</pubDate>
</item>
like image 356
fnev.eu Avatar asked Nov 08 '22 04:11

fnev.eu


1 Answers

Twig can remove whitespace characters in some cases, but it never adds them. So most obvious and simple solution is just to indent the included template.

Whitespace is not further modified by the template engine, so each whitespace (spaces, tabs, newlines etc.) is returned unchanged.

You can post-process the result XML. For example you can load it into the DOMDocument and dump with formatOutput option. Or through the tidy. Anyway it requires a some additional work.

But (IMHO), Twig is not a right tool for XML processing. Better use any standard API to build XML, such as DOMDocument (with formatOutput) or XMLWriter (with setIndent()).

like image 55
Timurib Avatar answered Nov 15 '22 10:11

Timurib