how to use break in for loop of swig template node js? - swig-template

Hello i am trying to stop my loop when it reaches to half of the total length.
just like using break it is not happening.
{% if page.member && page.member.length > 0 %}
{% for member in page.member %}
{{ member }}
{% if loop.index0 == ((page.member.length/2)-1) %}
{% set count = loop.index %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
please look into it.
Thanking you.

There is no {% break %} tag in Swig templates.
You don't need the break tag:
{% if page.member && page.member.length > 0 %}
{% set count = false %}
{% for member in page.member %}
{% if not count %}
{{ member }}
{% if loop.index0 == ((page.member.length/2)-1) %}
{% set count = loop.index %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}

Related

Comparison string in jinja

how to compare the data from database to compare with value in html with jinja?
I try gender value from db is Male
{% set gender=user.gender %}
{% if gender == 'Male' %}
{% set check = "checked" %}
{% else %}
{% set check = "unchecked" %}
{% endif %}
but the output is Male unchecked and Female also unchecked
I cannot reproduce the issue so it probably means your user variable is not defined as you expect:
import jinja2
t=jinja2.Template(''' {% set gender=user.gender %}
{% if gender == 'Male' %}
{% set check = "checked" %}
{% else %}
{% set check = "unchecked" %}
{% endif %}
''')
for gender in ['Male', 'Female']:
m = t.make_module(vars = {'user': {'gender': gender }})
print(m.check)
prints:
checked
unchecked

Nested navigation in eleventy

My data is creating a number of paginated posts. My front matter is as follows:
---
pagination:
data: cms
size: 1
alias: article
addAllPagesToCollections: true
layout: "_base.njk"
tags: articles
permalink: "/articles/{{ article.title | slug }}/"
eleventyComputed:
title: "{{ article.title }}"
eleventyNavigation:
key: "{{ article.title}}"
parent: "{{articles}}" //<-- how do I set the parent?
---
I would like my nav HTML structure to read
<ul>
<li>articles</li>
<ul>
<li>post-1</li>
<li>post-2</li>
</ul>
</ul>
Thanks in advance.
You can create hierarchy by using macro's.
But there's an another way: flatten that hierarchy and assign to each node a variable that describes the nesting level.
Then a similar code (as below) can be used to render that hierarchy:
{% set level = 0 %}
{% for category in collections.categoryCollection %}
{% set ulCount = (category.level - level) | abs %}
{% for i in range(0, ulCount) %}
{% if category.level > level %}
<ul>
{% else %}
</ul>
{% endif %}
{% endfor %}
<li>
<a href="{{ category.categoryUri | url | normalize | firstPage(globals.categories.category.postPageSize) }}">
{{ category.categoryName }} ( {{category.items.length}} / {{ category.allItems.length }} )
</a>
</li>
{% set level = category.level %}
{% endfor %}
{% for i in range(0, level) %}
</ul>
{% endfor %}
Hope this helps, I know it works, used them to get something like this:

How can add multiple collection in shopify liquid for loop?

I want to show products from the collections selected from theme setting.
Here is my code:
{% assign col1 = collections[settings.collection1] %}
{% assign col2 = collections[settings.collection2] %}
{% assign col = [col1, col2] %}
{% for product in col .products %}
{{ product }}
{% endfor %}
Your code should be like this
{% for i in (1..2) %}
{% capture col %}collection{i}{% endcapture %}
{% for product in collecitons.settings[col] %}
{{product}}
{% endfor %}
{% endfor %}

Twig checkbox is checked

please how can I do something as follows with twig checkbox
<input type="checkbox" name="checkbox">
{% if checkbox is checked %}
#do_it
{% else %}
#do_that
{% endif %}
help me, please
Checkboxes return string value.
{% if node.field_mycheckbox.value == '1' %}
<p>print 1</p>
{% else %}
<p>print 0</p>
{% endif %}
The controller:
$dispo = $mezzo->getAuDisponibile ();
// table column AuDisponibile is boolean
...
...
return $this-render ('anagrafiche/automezzi.html.twig', ['dispo' => $dispo]);
The template:
{% if dispo %}
<input input type='checkbox' name="idisponibile" value="SI" checked>
{% else %}
<input input type='checkbox' name="idisponibile" value="SI">
{% endif %}
You can check if a form field is checked with in example,
{% if form.field.children.KEY.vars.checked %}
Do something here
{% endif %}
Using it inside a loop.
{% for child in form.field %}
<input type="checkbox" name="checkbox" {{ child.vars.checked == true ? "Checked" }}>
{% endfor %}
or
<input type="checkbox" name="checkbox" {{ form.field.children.KEY.vars.checked ? "Checked" }}>
KEY being the array key.

How to Filter all Blog.Articles by containing specific tag in Shopify?

hello is there anyone shopify experts out there .
my only goal is to filter or display all articles according to their tag .
so this is what i have so far from this forum
{% assign counter = 0 %}
{% for article in blogs['lookbook'].articles %}
{% if article.tags contains 'Fox Racing' and counter < 2 %}
{% assign counter = counter | plus: 1 %}
<div class="njohn_search_otherpage">
<a href="{{ article.url }}" title="{{ article.title | escape }}">
<div>{{ article.image.src | img_url: 'medium' | img_tag: article.title }}</div>
<div>{{ article.title }}</div>
</a>
</div>
{% endif %}
{% endfor %}
the code are working but it's only four article will showed up. but in that sample tag i have already 10 articles.
The above code has a counter that limits the articles displayed, removing that counter works as expected, filtering all the articles by a tag.
{% for article in blogs['lookbook'].articles %}
{% if article.tags contains 'Fox Racing' %}
<div class="njohn_search_otherpage"> <div>{{ article.image.src | img_url: 'medium' | img_tag: article.title }}</div> <div>{{ article.title }}</div> </div>
{% endif %}
{% endfor %}

Resources