Twig For numeric - variables

I would like to know how how to make a like java in twig
java
i=10;
for(int x=0;x<i;x++){}
I found this on twig:
{% for i in 0..10 %}
* {{ i }} {% endfor %}
But i don't know how to change that 10 to a int variable in twig.

Just place the variable in your for?
{% set i = 10 %}
{% for j in 1..i %}
{{ j }}
{% endfor %}

Related

Get last article - Shopify / Liquid

I'm trying to retrieve the most recent article on a blog. My current code below does not output anything.
{% for article in blogs['myblog'].articles.last %}
{{ article.title }}
{% endfor %}
You don't need a loop in order to access the last item.
{% assign article = blogs['myblog'].articles.last %}
This will set article to the last item. You can then use it as expected.
{{ article.title }}
Docs: https://shopify.dev/docs/themes/liquid/reference/filters/array-filters#last
It can be done like this using forloop.last:
{% for article in blogs['myblog'].articles %}
{% if forloop.last == true %}
{{ article.title }}
{% endif %}
{% endfor %}
This assumes that blogs is a variable. Otherwise, try replacing blogs['myblog'].articles with blog.articles.

Shopify LinkList Loop Using Dynamic Variables

I have a block of code that I use on several pages. The only thing that changes in this block of code is the variable for the linklist. How do I refactor this code so that I can use a variable instead of static code?
<div class="featured-collections">
<div class="frow justify-start">
{% for link in linklists.book.links %}
<div class="collection-thumb">
<img class="collection-thumb-img" src="{{ link.object.featured_image | img_url: 'medium' }}">
<h3 class="collection-thumb-title">
{{ link.object.title | escape }}<br/>
</h3>
{{ link.object.price | money }}<br/>
<p>Book Now</p>
</div>
{% endfor %}
</div>
</div>
I tried this:
// Code for Variable Component - variable-linklist.liquid
{% for link in linklists.c.links %}
...
{% endfor %}
// Code for Book Page - page.book.liquid
{% assign c = "book" %}
{% include "variable-linklist" with "c" %}
but this did not work.
Code for Book Page - page.book.liquid
{% assign c = "book" %}
{% include "variable-linklist" with linklistHandle: c %}
Code for Variable Component - variable-linklist.liquid
{% for link in linklists[linklistHandle].links %}
...
{% endfor %}
or
Code for Book Page - page.book.liquid
{% assign c = "book" %}
{% include "variable-linklist" with c %}
Code for Variable Component - variable-linklist.liquid
{% for link in linklists[variable-linklist].links %}
...
{% endfor %}
When you use just with c the value is assigned to the variable with a name that is equal to the snippet name.

Can i have group_vars used in Jijnja2 template?

I have the following variable that i want to use in Jinja2 template and i am not sure if it is even possible
tag: pony
This is how I tried using it:
{% if ansible_fqdn in groups['machines-{{ tag }}'] %}
{% for host in groups['machines-{{ tag }}'] %}
echo "Do some magic with my {{ tag }}"
{% endfor %}
{% endif %}
Is it possible ? And how of course :)
Thanks !
This works for me
{% set group = 'machine-' + tag %}
{% if ansible_fqdn in groups[group] %}
{% for host in groups[group] %}
echo "Do some magic with my {{ tag }}"
{% endfor %}
{% endif %}

shopify pass a variable to settings

i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks
Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}

Increment in volt showing error

{% set counter = 0 %}
{% for remain_todolist in remain_todolists %}
{% if counter == countRem_todolist %}
['{{ remain_todolist.Project_Name }}', {{ remain_todolist.Remaining_Todos }}]
{% else %}
['{{ remain_todolist.Project_Name }}', {{ remain_todolist.Remaining_Todos }}],
{% endif %}~
{% do counter++ %}//Showing Error
{% endfor %}
Volt Increment statement is showing error
"Unknown expression 279"
What i am doing wrong ?
Have you tried without the do keyword?
Anyway, Volt loops already have counters available to use (see Loop Context), here's a version using it:
{% for list in remain_todolists %}
['{{ list.Project_Name }}', {{ list.Remaining_Todos }}]{{ loop.last ? '' : ',' }}
{% endfor %}
As #cvsguimaraes suggest you can use Loop context which should be better in your case.
I don't think that do statement is valid in Volt. If you need counter value outside loop you can increment value just like that:
{% set counter = counter + 1 %}