Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Slider with thumbnails - select next item in for loop

Started by Muut Archive 10 years ago · 4 replies · 1368 views
10 years ago

Hey guys,

I'm trying to build a slideshow with thumbnails of the next and previous slide. The Idea is to show the thumbnail of the next slide only if there is a next slide and vice versa. I hope you get the idea…

Images are set inside the page-header of the markdown file like this:

YAML
swiper_class: swiper
gallery:
  - image: bsp1.jpg
    description: quia non numquam
  - image: bsp2.jpg
    description: Sed ut perspicia
  - image: bsp3.jpg
    description: Sed ut perspicia

My template file for the slider looks like the following. It seems to work but since I'm no PHP developer I don't quite know if thats the right way to do. It would be nice if someone could have a look at my code and give some advice. Thanks in advance. :)

TWIG

  <div class="swiper-wrapper">
        {% for element in page.header.gallery %}
            <div class="swiper-slide">
                {% if element.description  %} 
                <p class="description">{{ element.description|e }}</p>
                {% endif %}
                <img src="{{ page.media[element.image].cropZoom(1440, 800).url() }}" >

                {# Add Pagination #}
                {# show next thumb only if there is a next slide #}
                {% if (loop.index < loop.length ) %}
                  {% set next_image = page.header.gallery[loop.index0 + 1] %}
                  <figure class="swiper-thumbnail-next">
                    <img src="{{ page.media[next_image.image].cropZoom(150, 100).url() }}" >
                    <figcaption>next</figcaption>
                  </figure>
                {% endif %}

                {% if (loop.index > 1 ) %}
                {# show prev thumb only if there is a prev slide #}
                {% set prev_image = page.header.gallery[loop.index0 - 1] %}
                <figure class="swiper-thumbnail-prev">
                  <img src="{{ page.media[prev_image.image].cropZoom(150, 100).url() }}" >
                  <figcaption>prev</figcaption>
                </figure>
                {% endif %}
            </div>

        {% endfor %}
    </div>
---
10 years ago

i am newbie and i am so interesting with your post...in which way i can see your template by using script above?&quest;?

10 years ago

Also interested in seeing the finished product... please post the url when it goes live :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1349 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1118 9 years ago