Resolve liquid variable inside liquid tag

会有一股神秘感。 提交于 2019-12-08 09:10:56

问题


I'm using Octopress, which is a framework for Jekyll to render my site. I'm using a plugin which wraps the ruby-aaws gem, allowing queries to Amazon using Amazon's product identifier (asin). I'd like to render a portion of the page recusively, looping through a list of asins to produce the output. Here's my code so far:

<section>
  <h1>Recent Diversions</h1>
  {% for asin in ["044656432X", "0743276396", "B001YT048E"] %}
    {% capture a_image %}{{ asin | amazon_medium_image }}{% endcapture %}
    {% capture a_link %}{{ asin | amazon_link }}{% endcapture %}
    {% capture a_authors %}{{ asin | amazon_authors }}{% endcapture %}

    <p>{{ a_image }}</p>
    <p>{{ a_link }} by {{ a_authors }}</p>
  {% endfor %}
</section>

My understanding is the {% capture variable_name %}...{% endcaputre %} renders what is encapsulated and assigns the result to variable_name. However, when I generate the site nothing is generated. If I substitue a single asin for the asin references within the capture tags the page renders properly.

How should I properly reference the asin variable inside the capture tag to make this work?


回答1:


I have finally sorted this out. The code shown above is in a file called aws.html, which is included in the main index.html for the site. It seems that Liquid doesn't allow variable assignments at that level. When I move the variable assignment to the _config.yml file, i.e., asins: ["044656432X", "0743276396", "B001YT048E"] then the code above works perfectly.



来源:https://stackoverflow.com/questions/7259385/resolve-liquid-variable-inside-liquid-tag

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