2
votes

IN SHORT

how do I get this assign variable into the for tag?

{% assign thisslider = include.whichslider %} // returns "slider1"
{% for item in thisslider %}

CONTEXT

On my blog site, I'm trying to make it so I can post images to show in the article via sliders. It works for just one slider, but when I want more than one slider, I want to be able to specify which slider within the include;

{% include content-slides.html whichslider="slider1" %}

In content-slides.html I need to get the include parameter "slider1" and pass it into the for loop declaration something like below;

{% assign thisslider = include.whichslider %}
    {% for item in page.thisslider %}
        <p>each slide code in here</p>
    {% endfor %}
</div>

So the for loops through the slider defined in the include parameter (referenced in the post front matter)

post.md

---
layout: default
slider1:
  item1 :
    image : "xe-1.jpg"
    alt : "foo"
    caption : "this is the caption"
  item2 :
    image : "xe-2.jpg"
    alt : "foo"
    caption : "this is the caption for second slide"
slider2:
  item1 :
    image : "xe-3.jpg"
    alt : "foo"
    caption : "this is the caption"
  item2 :
    image : "xe-4.jpg"
    alt : "foo"
    caption : "this is the caption for second slide"
---
{% include content-slides.html whichslider="slider1" %}
{% include content-slides.html whichslider="slider2" %}

content-slides.html

{% assign thisslider = include.whichslider %}
{% for item in page.thisslider %}
   <p>each slide code in here</p>
{% endfor %}
1

1 Answers

2
votes

Replace dot notation :

{% for item in page.thisslider %}

By bracket notation

{% for item in page[thisslider] %}