5

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>
fnev.eu
  • 303
  • 2
  • 10
  • Is it just white space you need `visually` or do you want spaces (indentation) in the source code (view source in browser)? – Alvin Bunk Dec 20 '16 at 17:01
  • @AlvinBunk as we are in an XML feed, it's the same in this case. But it's for the source code, yep. – fnev.eu Dec 20 '16 at 17:05
  • 1
    This will be a pain. You'd better create a function that will take your rss as input and will return your beautified rss as output. For example: `{{ beautify_xml(include("hello.rss.twig")) }}` where `beautify_xml` will be in a twig extension with [this implementation](http://stackoverflow.com/a/3616722/731138). – Alain Tiemblo Dec 21 '16 at 00:18
  • Thx @AlainTiemblo for your comment. Seems to be "overkill" just for good indentation... I don't want to lose in performance for that, so I'll keep what I currently have. – fnev.eu Dec 21 '16 at 16:08
  • There are low-level extensions to do that, like "tidy html" and there are probably others. I developed some code generators and believe me, I prefer running the cs-fixer post generation than taking care of cs when writting my templates. – Alain Tiemblo Dec 21 '16 at 16:13

1 Answers1

1

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()).

Timurib
  • 2,735
  • 16
  • 29