在每五个高尔夫球场之后使用Jinja循环阵列添加广告

时间:2017-04-03 16:01:49

标签: javascript python loops jinja2

问题

我有两个看似完全相同的模板,但branson.html广告在每五个高尔夫球场后正确显示,但在ozarks.html它们没有。我在这个问题的最底部发布了以前StackOverflow question的解决方案。

我做了什么

  • 我已更改Google表格中的列类型,其中数据来自,自动转换为纯文本
  • 我甚至尝试过复制和粘贴HTML结构 branson.htmlozarks.html,然后只需更改标签
  • 我确保fab text update
  • 我确保查看高尔夫球场对象以查看course.region == "Ozarks"

ozarks.html(每隔五个课程后不显示广告)

<div class="featured">

    <div class="courses courses--featured">

        {% set count = 0 %}
        {% for content in COPY.courses %}
        {% if content.region == "Ozarks" %}
                <div class="course course--featured featured--region">
                    {% if content.featured == "Featured" %}
                        <a href="/courses/{{ content.slug }}"><img src="" class="course__image image--region"></a>
                    {% endif %}

                    <div class="course__inner inner--region">
                        <div class="course__wrapper">
                            {% if content.state == "MO" %}
                                <p class="course__state">Missouri</p>
                            {% elif content.state == "IL" %}
                                <p class="course__state">Missouri</p>
                            {% endif %}
                        </div>

                        <div class="course__wrapper">
                            <a href="/courses/{{ content.slug }}"><p class="course__name name--region">{{ content.name }}</p></a>
                        </div>

                        {% if content.featured == "Featured" %}
                            <p class="course__desc">{{ content.description }}</p>
                        {% endif %}
                    </div>
                </div>

        {% set count = count + 1 %}
        {% if loop.index % 5 == 0 %}
        <div class="advertising advertising--inline">
            <div class="ad ad--rect">

                <div class="text-center hidden-xs">
                    <div id="fixed-leaderboard-region-top"
                        class="dfp-ad"
                        data-dfp-custom-pos="fixed-leaderboard-top, htf"
                        data-dfp-size="[728,90]">
                    </div>
                </div>

                <div class="text-center hidden-sm hidden-md hidden-lg">
                    <div id="fixed-leaderboard-region-top-mobile"
                        class="dfp-ad"
                        data-dfp-custom-pos="fixed-leaderboard-top, htf"
                        data-dfp-size="[320,50]">
                    </div>
                </div>
            </div>
        </div>
        {% endif %}
        {% endif %}
        {% endfor %}
    </div>
</div>

branson.html(工作正常。)

<div class="featured">
    <div class="courses courses--featured">

        {% set count = 0 %}
        {% for content in COPY.courses %}
        {% if content.region == "Branson" %}
                <div class="course course--featured featured--region">
                    {% if content.featured == "Featured" %}
                        <a href="/courses/{{ content.slug }}"><img src="" class="course__image image--region"></a>
                    {% endif %}

                    <div class="course__inner inner--region">
                        <div class="course__wrapper">
                            {% if content.state == "MO" %}
                                <p class="course__state">Missouri</p>
                            {% elif content.state == "IL" %}
                                <p class="course__state">Missouri</p>
                            {% endif %}
                        </div>

                        <div class="course__wrapper">
                            <a href="/courses/{{ content.slug }}"><p class="course__name name--region">{{ content.name }}</p></a>
                        </div>

                        {% if content.featured == "Featured" %}
                            <p class="course__desc">{{ content.description }}</p>
                        {% endif %}
                    </div>
                </div>

        {% set count = count + 1 %}
        {% if loop.index % 5 == 0 %}
        <div class="advertising advertising--inline">
            <div class="ad ad--rect">

                <div class="text-center hidden-xs">
                    <div id="fixed-leaderboard-region-top"
                        class="dfp-ad"
                        data-dfp-custom-pos="fixed-leaderboard-top, htf"
                        data-dfp-size="[728,90]">
                    </div>
                </div>

                <div class="text-center hidden-sm hidden-md hidden-lg">
                    <div id="fixed-leaderboard-region-top-mobile"
                        class="dfp-ad"
                        data-dfp-custom-pos="fixed-leaderboard-top, htf"
                        data-dfp-size="[320,50]">
                    </div>
                </div>
            </div>
        </div>
        {% endif %}
        {% endif %}
        {% endfor %}
    </div>
</div>

早期解决方案

{% set count = 0 %}
{% for content in COPY.courses %}
{% if content.featured == "Test" %}
<div class="course course--featured">
    <a href=""><img src="" class="course__image image--home"></a>
    [...]
    </div>
</div>

{% set count = count + 1 %}
{% if count % 5 == 0 %}
    <!-- additional content once every 5 courses -->
{% endif %}
{% endif %}
{% endfor %}

1 个答案:

答案 0 :(得分:0)

这很容易解决。

ozarks.html模板上,它使用了loop.index而不是count设置得更高,以确定广告的放置位置。