Jinja2 and Bootstrap carousel - “item active”

孤人 提交于 2021-02-08 10:22:54

问题


I'm new to Jinja and this is my first post here on Stack Overflow. I'm trying to loop through a gallery of images handled by bootstrap carousel/modal. I was able to let it work; <img> and <div> are rendered correctly, however I can't loop through the active element. Searching the web, I found that macros can help achieve it but I'm not familiar with using them. Here's the code I'm working on:

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


Jinja for loops have counters so you can check if you are on the first iteration of the loop and make that one the active slide.

Something like this:

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


来源:https://stackoverflow.com/questions/33677374/jinja2-and-bootstrap-carousel-item-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!