Jinja2和Bootstrap轮播 - “项目有效”

时间:2015-11-12 17:16:47

标签: python html twitter-bootstrap jinja2

我是Jinja的新手,这是我在Stack Overflow上发表的第一篇文章。 我正在尝试循环通过bootstrap carousel / modal处理的图像库。我能够让它发挥作用; <img><div>已正确呈现,但我无法遍历活动元素。在网上搜索,我发现宏可以帮助实现它,但我不熟悉使用它们。这是我正在处理的代码:

<div class="modal fade" id="myModalGal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-body">
            <!-- Wrapper for slides -->
            <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                <div class="carousel-inner">
                    <div class="item active">
                        {% for content in porte %} 
                        {% if content.gal_porte %} 
                        <img src="{{content.gal_porte}}" class="img-responsive" alt="test">
                        <div class="carousel-caption"></div>
                    </div>
                    <div class="item">
                        {% elif content.gal_porte %} <img src="{{content.gal_porte}}" class="img-responsive" alt="test1">
                        <div class="carousel-caption"></div>
                        {% endif %} 
                        {% endfor %}
                    </div>
                    <!-- Controls -->
                    <a class="home-icon left" href="#carousel-example-generic" role="button" data-slide="prev"> <i class="fa fa-arrow-left"></i>
                    </a> <a class="home-icon right" href="#carousel-example-generic" role="button" data-slide="next"> <i class="fa fa-arrow-right" style="text-align: right;"></i>
                    </a>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:3)

Jinja for循环有计数器,因此你可以检查你是否在循环的第一次迭代中并使那个循环成为活动幻灯片。

这样的事情:

<div class="carousel-inner">
  {% for content in porte %}
  <div class="item{% if loop.index == 1 %} active{% endif %}">
    <img src="{{content.gal_porte}}" class="img-responsive" alt="test1">
    <div class="carousel-caption"></div>
  </div>
  {% endfor %}
  <!-- Controls -->
  <a class="home-icon left" href="#carousel-example-generic" role="button" data-slide="prev"> 
    <i class="fa fa-arrow-left"></i>
  </a> 
  <a class="home-icon right" href="#carousel-example-generic" role="button" data-slide="next"> 
    <i class="fa fa-arrow-right" style="text-align: right;"></i>
  </a>
</div>