php - Twig在模板生成后添加内容

标签 php mysql templates twig

我有一个 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” }

输出

Picture of the Output

最佳答案

因此,您必须将 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/

相关文章:

c++ - 修复警告 "comparison is always false due to limited range of data type [-Wtype-limits]"

php - 计算有多少客户有 1 项服务,有多少客户有 2 项服务

php - 如何通过 jQuery 将文件发送到 php?

php - 如何使用 Zend_Http_Client 模拟 POSTing 多个复选框表单字段 myField[]?

PHP 行以与 Mysql 相反的顺序插入

python - 在 Django 模板中迭代 Expando 的动态属性

PHP解析自定义平​​面文本数据库

php - 无法从 PHP 创建 View 但可以从 phpMyAdmin

mysql - 如何通过一次查询从另一个表的数据更新一个表的所有行字段?

c++ - 声明模板结构的实例