我有一个 twig 模板,它可以动态生成 31 个 Accordion 项,并引用当前日期。 (一个循环即可完成这项工作) 意味着今天 2014-02-10 将有 14 个项目,从 2014-02-10 开始,然后 2014-02-09、2014-02-8 ...回到 2014-02-27。
我要将内容添加到这些 Accordion 项目中。
当然内容属于特定日期。
我问自己添加内容的最佳实践是什么,因为模板是一个创建所有 31 个项目的循环?
与 <IF>
合作在模板的循环内?
使用简单的 CMS 等生成模板后添加内容。 ?如果是的话怎么做?
不同的策略?
内容变化很大。每天都会向项目添加和删除内容。
谢谢
佐姆
//编辑
.tmpl代码
<section class="ac-container">
{% for key,value in resultArr %}
<div>
<input id="ac-{{key}}" name="accordion-1" type="radio" {% if loop.first %}checked{% endif %} />
<label for="ac-{{key}}">{{value}}</label>
<article class="ac-large">
<figure>Some content... </figure>
</article>
</div>
{% endfor %}
</section>
结果Arr:
array(15) {
[10]=> 字符串(10)“2014-02-10” [9]=> 字符串(10)“2014-02-09” [8]=> 字符串(10)“2014-02-08” [7]=> 字符串(10)“2014-02-07” [6]=> 字符串(10)“2014-02-06” [5]=> 字符串(10)“2014-02-05” [4]=> 字符串(10)“2014-02-04” [3]=> 字符串(10)“2014-02-03” [2]=> 字符串(10)“2014-02-02” 1 => 字符串(10)“2014-02-01” [31]=> 字符串(10)“2014-01-31” [30]=> 字符串(10)“2014-01-30” [29]=> 字符串(10)“2014-01-29” [28]=> 字符串(10)“2014-01-28” [27]=> 字符串(10)“2014-01-27” }
输出
最佳答案
因此,您必须将 resultArr
准备为如下结构:
$resultArr = array(
array(
'acKey' => '10',
'date' => '2014-02-10',
'content' => 'Your content for 2014-02-10 date'
),
array(
'acKey' => '09',
'date' => '2014-02-09',
'content' => 'Your content for 2014-02-09 date'
),
// and so on
)
之后,您必须将 Twig 模板更改为:
<section class="ac-container">
{% for element in resultArr %}
<div>
<input id="ac-{{ element.acKey }}" name="accordion-1" type="radio" {% if loop.first %}checked{% endif %} />
<label for="ac-{{ element.acKey }}">{{ element.date }}</label>
<article class="ac-large">
<figure>{{ element.content }}</figure>
</article>
</div>
{% endfor %}
</section>
关于php - Twig在模板生成后添加内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21675052/