【问题标题】:Twig add content after template is generatedTwig在模板生成后添加内容
【发布时间】:2014-02-10 10:46:44
【问题描述】:

我有一个 twig 模板,它动态生成 31 个手风琴项目,并引用当前日期。 (一个循环完成这项工作) 意味着今天 2014-02-10 将有 14 个项目,从 2014-02-10 开始,然后是 2014-02-09、2014-02-8 ... 回到 2014-02-27。

我将在这些手风琴项目中添加内容。

当然内容属于特定日期。

我问自己添加内容的最佳做法是什么,因为模板是一个创建所有 31 个项目的循环?

在模板循环中使用<IF>? 使用简单的 CMS 或其他东西生成模板后添加内容。 ?如果是怎么办?

不同的策略?

内容变化很大。每天都会在项目中添加和删除内容。

谢谢

Zomh

//编辑

.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>

resultArr:

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

【问题讨论】:

  • 抱歉,我很难理解您的问题...您能提供您当前的代码吗?
  • 做了一些编辑,希望现在清楚

标签: php mysql templates twig


【解决方案1】:

所以,你必须准备好你的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
)

之后,您必须将您的树枝模板更改为:

<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>

【讨论】:

    猜你喜欢
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多