Short version:
I want to add 1 to a number in a liquid template and use the result as an array index.
{% capture plus_one %}{{ 0 | plus: 1 }}{% endcapture %}
<div>-Value of plus_one: {{plus_one}}</div>
<div>-This works: {{site.posts[1].title}}</div>
<div>-This doesn't: {{site.posts[plus_one].title}}</div>
Result:
-Value of plus_one: 1
-This works: The Zone
-This doesn't:
Long version:
I'm using Jekyll, with no plugins. I want to give the current post a link to the next post that is in the same category. (The category is hardcoded to 'journal' in this code.)
My code loops over all posts in the category array, looking for the current post. When it is found, I try to grab the next post in the category array.
{% for num in (0..site.categories.journal.size) %}
{% assign page2 = site.categories.journal[num] %}
{% if page2.title == page.title and page2.date == page.date %}
{% capture plus_one %}{{ num | plus: 1 }}{% endcapture %}
{% endif %}
{% endfor %}
<div>value of plus_one: {{plus_one}}</div>
<div>This doesn't work: {{site.categories.journal[plus_one].title}}</div>
<div>This does: {{site.categories.journal[1].title}}</div>
Result:
<div>value of plus_one: 1</div>
<div>This doesn't work: </div>
<div>This does: A Blog Post Title</div>
I guess the value of my variable 'plus_one' is being treated as a string instead of a number.
Is there any way to convert it to a number?
Or is there another way to achieve what I'm trying to do?
Class: Liquid::Break Break tag to be used to break out of a for loop.
You can directly create a new empty array controllers and concat to it your controllerName converted into an array using the workaround split:'' . The result is directly an array, without the extra string manipulations.
To create the product array we start with the capture tag so that we can set the new product_list variable equal to all our product information. When capturing the values we separate each value associated with a single product using a | and each set of product values with ::.
I was having a similar problem, using the answers above I got the following working on my site. Might be of help to someone else.
{% for num in (0..site.categories.work.size) %}
{% assign page2 = site.categories.work[num] %}
{% if page2.title == page.title and page2.date == page.date %}
{% assign next_i = forloop.index0 | plus: 1 %}
{% assign prev_i = forloop.index0 | minus: 2 %}
{% if next_i == site.categories.work.size %}
{% assign next_i = 1 %}
{% endif %}
{% endif %}
{% endfor %}
<nav class="pagination">
<div class="next">
<a href="work/{{site.categories.work[next_i].slug}}/">Next</a>
</div>
<div class="previous">
<a href="work/{{site.categories.work[prev_i].slug}}/">Previous</a>
</div>
</nav>
assign
preserves the number:
{% for item in items %}
{% assign next_i = forloop.index0 | plus: 1 %}
{% assign prev_i = forloop.index0 | minus: 1 %}
{{ items[next_i] }}
{{ items[prev_i] }}
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With