gitan
gitan

Reputation: 23

Jinja2 and Bootstrap carousel - "item active"

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>

Upvotes: 1

Views: 3177

Answers (1)

sponrad
sponrad

Reputation: 690

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>

Upvotes: 4

Related Questions