假设我有以下_teaser.twig partial:
<article>
<h2> {{ article.headline }} </h2>
<p> {{ article.lede }} </p>
{% if {{article.byline }} %}
<address>{{ article.byline }}</address>
{% endif %}
</article>其中包括以下几种方式:
<aside>
{% for article in teasrs %}
{% include '_teaser.twig' %}
{% endfor %}
</aside>
<section>
{% for article in opinion.items %}
{% include '_teaser.twig' %}
{% endfor %}
<section>在json文件中包含以下数据结构:
{
"article": {
"headline": "A short headline",
"lede": "A short descriptive lede paragraph"accusantium"
},
"teasers": {
"article1": {
"headline": "Some headline",
"lede": "A lede that describes the article, but without revealing too much, so that users still have a reason to click"
},
"article2": {}
},
"opinion": {
"article": {
"byline": "Anonymous"
},
"items": {
"article1: {},
"article2: {}
}
}
}理想情况下,我希望变量解析增加作用域,就像在Mustache中一样。
所需的输出为:
<aside>
<article>
<h2> Some headline </h2>
<p>
A lede that describes the article,
but without revealing too much, so that
users still have a reason to click
</p>
</article>
<article>
<h2> A short headline </h2>
<p> A short descriptive lede paragraph </p>
</article>
</aside>
<section>
<article>
<h2> A short headline </h2>
<p> A short descriptive lede paragraph </p>
<address>Anonymous</address>
</article>
<article>
<h2> A short headline </h2>
<p> A short descriptive lede paragraph </p>
<address>Anonymous</address>
</article>
<section>除非我遗漏了什么,否则使用default()过滤器指定默认内容是不可取的,因为我希望默认内容派生自模型,而不是视图。例如,在预告中,我不想有一个备用署名,但在评论部分,我希望总是显示一个署名,带有备用到Anonymous的署名,对于这一点,使用default无济于事。
发布于 2015-02-23 22:16:55
使用默认管道
文档:http://twig.sensiolabs.org/doc/filters/default.html
<article>
<h2> {{ article.headline|default('default value) }} </h2>
<p> {{ article.lede|default('default value) }} </p>
</article>https://stackoverflow.com/questions/28675393
复制相似问题