fne*_*.eu 5 indentation symfony twig
我尝试在 Symfony 项目中制作一个漂亮的 RSS Feed。对于每个项目,我包含一个文件。没关系,但是当我查看输出时,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>Space Raclette</title>
<description></description>
<language>fr</language>
<lastBuildDate>Wed, 30 Nov 2016 11:22:45 +0100</lastBuildDate>
<item>
<title>Topic de l'?©t?© du Capitaine Crochet 2</title>
<link>...</link>
<guid isPermaLink="false">.../39fa</guid>
<description></description>
</item>
<item>
<title>Topic de l'?©t?© du Capitaine Crochet</title>
<link>...</link>
<guid isPermaLink="false">.../39fa</guid>
<description></description>
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能在“项目文件”中保持缩进而不出现错误的缩进?我尝试使用spaceless和-,但没有成功。
如果可以的话,这里是我的文件。
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>
Run Code Online (Sandbox Code Playgroud)
索引.rss.twig
{% extends 'RSSBundle::layout.rss.twig' %}
{% block content %}
{% for publication in web_publications %}
{{ include('RSSBundle:Publication:_single.rss.twig') }}
{% endfor %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
_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>
Run Code Online (Sandbox Code Playgroud)
在某些情况下, Twig可以删除空白字符,但它永远不会添加它们。因此,最明显和简单的解决方案就是缩进包含的模板。
模板引擎不会进一步修改空白,因此每个空白(空格、制表符、换行符等)都会原样返回。
您可以对结果 XML 进行后处理。例如,您可以DOMDocument使用选项将其加载到并转储formatOutput。或者通过tidy. 无论如何,它需要一些额外的工作。
但是(恕我直言),Twig 并不是一个适合 XML 处理的工具。最好使用任何标准 API 来构建 XML,例如DOMDocument(with formatOutput) 或XMLWriter(with setIndent())。