Jinja2 multiple variables in same for loop - for-loop

I would like to populate the same section of a Jinja2 for loop with data from 2 separate SQL queries using Python / Webapp2 / Jinja2.
Specifically, I am storing team info in a variable called "team" and score info in a variable called "wins". I need to position some data from the wins variable directly after data from the team variable but cannot figure out how to do this within a for loop.
Here is a simplified version of what I am trying to achieve:
{% block content %}
{% for team in team %}
<div>{{ team[0] }} record: {{ wins[1] }}</div>
<div>{{ team[1] }} and {{ team[2] }}</div>
<div>{{ team[3] }}</div>
{% endfor %}
{% endblock %}
What is the best way to do this? I need to get that "wins" variable called but cannot determine how. Any help is appreciated.

I usually zip them up into a list of tuples in the view function. Make sure the teams and wins are sorted correctly beforehand.
team_info = zip(teams, wins)
And then you can access the tuples as you iterate over the list in the template
{% block content %}
{% for team, win in team_info %}
<div>{{ team[0] }} record: {{ wins[1] }}</div>
<div>{{ team[1] }} and {{ team[2] }}</div>
<div>{{ team[3] }}</div>
{% endfor %}

Related

How can I arrange an array of times in ascending order using Twig?

I have a list of dates and times in the CMS that need to be organised in ascending order.
What is it that I need to check for?
I think I need to use |sort but the documentation for twig isn't really clear on how to use it for a newbie to development.
{% if inspectionDates|length %}
<h4 class="column__title">Inspection times</h4>
<ul class="column__timelist">
{% for inspection in inspectionDates %}
<li>
<span>{{ inspection.date|date('l j F') }}</span>
<span class="column__hours">{{ inspection.startTime|date('g:i') }}<i>{{ inspection.startTime|date('A') }}</i> - {{ inspection.endTime|date('g:i') }}<i>{{ inspection.endTime|date('A') }}</i></span>
</li>
{% endfor %}
</ul>
{% endif %}
Can you please explain like I'm 5 how it works and the logic around sorting by date?

Liquid - if contains statement not working in for loop

I have a very basic conditional inside a loop that isn't working:
{% for tag in collections.all.tags %}
<li class="mb-1 ">
<a class="text-white" title="{{ tag }}" href="#">
{{ tag | handle }}
{% if canonical_url contains tag | handle %}
active
{% endif %}
</a>
</li>
{% endfor %}
Right now, my tag | handle is "dodge" and if I print my {{ canonical_url }} I get https://localhost:3000/collections/all/dodge so my conditional should evaluate to truthy and print the word 'active'.
If I modify my statement to {% if canonical_url contains 'dodge' %} it works fine but I need it to be dynamic. Any suggestions?
Set the handle into a variable and use the variable for the comparison instead of the handle.
First You have to assign value of tag handle to shopify liquid variable & then you will be able to use variable in condition And then every thing will be working fine & dynamic. Thanks !!

Shopify Liquid How do I use for-loop index variables in an assign or capture tag?

I am just getting into some liquid template coding on a Shopify site. How do I use the for-loop index/variable when defining variable names in assign or capture tags? ie: I'm trying to condense the code to create multiple link menus from sequentially numbered settings, where [i] would be a number between 2 and 4 in the settings key.
What is the proper syntax to insert that number into
a) a tag like an if statement or assign.
b) interpolated text like in the h3 element below.
c) a nested/bracketed key statement (sorry if that's not what its called, i'm still learning), like in the second for statement.
{% for i in (2..4) %}
{% if settings.footer_quicklinks_enable[i] %}
<div class="grid-item medium--one-half large--three-twelfths">
<h3>{{ 'layout.footer.quick_links{{i}}' | t }}</h3>
<ul>
{% for link in linklists[settings.footer_quicklinks_linklist[i]].links %}
<li>{{ link.title }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endfor %}
You need to use the square bracket notation instead of dot notation.
Create a string containing the variable name (with assign or capture), then use square bracket notation to access the setting with that name.
For example:
{% capture var %}footer_quicklinks_enable_{{i}}{% endcapture %}
{% if settings[var] %}
Also see this similar answer.

Store Twig loop for multiple reuse

I have two arrays that I loop through, but they are nested.
Reduced to the minimum, it looks like this:
{% for item in items %}
<label>{{ item.name }}</label>
<select>
{% for attribute in attributes %}
<option>{{ attribute.name }}</option>
{% endfor %}
</select>
{% endfor %}
The problem is the size of the arrays. There are about 1,100 items and 400 attributes. As one can guess, this is slow. Very slow.
Is it possible to "store" the inner loop, and just reuse the generated/rendered block?
Move the inner loop outside of the outer loop and then store the value generated in a Twig variable. Then run the outer loop supplying the variable between the <select> tags. This way you only generate the inner loop once.
{% set options = '' %}
{% for attribute in attributes %}
{% set options = options ~ '<option>' ~ attribute.name ~ '</option>' %}
{% endfor %}
{% for item in items %}
<label>{{ item.name }}</label>
<select>
{{ options }}
</select>
{% endfor %}

Translatable content and HTML tags

I use Twig and I want to make the following content translatable :
{% trans %}
You have actually <span class='messageNumber'>{{messageNumber}} message(s)</span> in your mailbox.
{% endtrans %}
But when this translatable content will be parsed by POEdit and sent to translators, they will see the <span> tags and attributes. What can I do to avoid this ?
I thought about doing this way :
{% messageNumberFormatted = "<span class='messageNumber'>"~messageNumber~"message(s)</span>" %}
{% trans %}
You have actually {{messageNumberFormatted}} in your mailbox.
{% endtrans %}
But isn't it a bit heavy or even bad practice for the translators ? In that case, they can't even see the word "message".
First, you should use transchoice with explicit interval pluralization, like this :
{% transchoice message_count %}
{0}You have {{no messages}} yet|{1}You have {{one message}}|]1,+Inf]You have {{%count% messages}}.
{% endtranschoice %}
Then maybe you could use replace to replace {{ with the opening tag, and }} with the closing tag. I don't know whether you can directly chain like this
{% transchoice message_count | replace('...') %}
Or if you must store in a variable by using set first.
You can use the trans twig filer with keys representing your sentences.
{{ you.have.actually|trans }} <span class='messageNumber'> {{ messageNumber message|trans }} </span> {{ in.your.mailbox|trans }}

Resources