robwierzbowski / jekyll-image-tag

Better images for Jekyll.
BSD 3-Clause "New" or "Revised" License
98 stars 26 forks source link

List values not being passed to the image template in a for loop #3

Closed stephenwil closed 11 years ago

stephenwil commented 11 years ago

Hi, My post contains a YAML list which I am iterating though in a for loop:

products:
 - product: Cake Box 1
   image : cake-boxes/cake-boxes.jpg
   price : 1.99
 - product: Cake Drum 1
   image : cake-drums/cake-drums.jpg
   price : 1.99
 - product: Cake Box 2
   image : cake-boxes/cake-boxes.jpg
   price : 1.99
 - product: Cake Drum 2
   image : cake-drums/cake-drums.jpg
   price : 1.99

I also have an include that parses through this list in a for loop :

{% for p in page.products %}
        {% image {{ page.categories }}/{{ p.image }}  %}
 {% endfor %}

However, the template only ever sees the first instance in the list - and this is for each iteration.

Any idea why?

robwierzbowski commented 11 years ago

Not sure at a glance. I know there are some issues parsing YAML variables in layouts, so that might be it.

If you do something like:

{% for p in page.products %}
        {% image {{ page.categories }}/{{ p.image }}  %}
        Image: {{ p.image }}
{% endfor %}

Does the loop go all the way through?

stephenwil commented 11 years ago

Yes. If I add <p> {{ p.image }} </p> into the loop, it gets rendered ok.

In the template, if it add puts @markup at the start of the render routine of the tag, on the first iteration, it outputs {{ page.categories }}/{{ p.image }}

but on the next 3 iterations, it outputs the text from the 1st item - e.g. 'cake-boxes/cake-boxes.jpg'

robwierzbowski commented 11 years ago

That is odd. Is the include in a post or a layout?

I don't have much free time this week, but I'll take a closer look next weekend. Please post anything you find and let me know if you solve the issue. It might be worth asking on the #jekyll channel in IRC.

Note: you can surround your HTML tags and code with backticks (`) so it's printed as code instead of rendered. Edited your post for you.

stephenwil commented 11 years ago

Hi, Think it's because the variable @markup on L34 is masking the scope of the @markup variable that's come in on the image call.

E.g. if I change L34 to @markup2 = Liquid::Template.parse(@markup).render(context).gsub(/{{|{\%/, '{{' => '{{', '{\%' => '{%')

and then on L39 to markup = /^(?:(?[^\s.:\/]+)\s+)?(?[^\s]+.[a-zA-Z0-9]{3,4})\s*(?[\s\S]+)?$/.match(@markup2)

then @markup is always in scope, and contains the correct markup from each iteration.

Fixed!

stephenwil commented 11 years ago

Thanks for commenting however.

robwierzbowski commented 11 years ago

Awesome! Would you submit a pull request with your changes?

stephenwil commented 11 years ago

Will do.