gpt4 book ai didi

symfony - 在 Twig 中使用 block 时如何保持缩进

转载 作者:行者123 更新时间:2023-12-01 06:05:02 25 4
gpt4 key购买 nike

我尝试在 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&#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>

我该怎么做才能在“项目文件”中保持缩进而不出现错误的缩进?我试着玩 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>

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>

最佳答案

Twig can remove whitespace在某些情况下,字符,但它从不添加它们。所以最明显和简单的解决方案就是缩进包含的模板。

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



您可以对结果 XML 进行后处理。例如,您可以将其加载到 DOMDocument并转储 formatOutput 选项。或通过 tidy .无论如何,它需要一些额外的工作。

但是(恕我直言),Twig 不是用于 XML 处理的正确工具。最好使用任何标准 API 来构建 XML,例如 DOMDocument (与 formatOutput )或 XMLWriter (与 setIndent() )。

关于symfony - 在 Twig 中使用 block 时如何保持缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41246694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com