I'm not sure why my code isn't executing. I know that it should be working this way but all that happens now is that it doesn't do the else portion.
In debugging I know descriptions is not null and descriptions show for those that have it.
{% if descriptions is not null %}
{{ dump(descriptions) }}
{% for description in descriptions %}
<td>{{ description.productDesciption }}</td>
{% endfor %}
{% else %}
<td>
Create a Description for this Product
</td>
{% endif %}
You can simplify using the The else Clause of the for statement:
{% for description in descriptions %}
<td>
{{ description.productDesciption }}
</td>
{% else %}
<td>
Create a Description for this Product
</td>
{% endfor %}
Hope this help
You can use if into for loop.
{% for description in descriptions if descriptions is not null %}
<td>
{{ description.productDesciption }}
</td>
{% else %}
<td>
Create a Description for this Product
</td>
{% endfor %}
Related
I am very new to twig and have a problem which I don't know how to solve.
So I have something like this:
{% for entry in entries %}
{% set startDate = entry.begin|date_time %}
% set endDate = entry.end|date_time %}
<tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
<td class="text-nowrap">
{% block date_begin %}{{ entry.begin|date_time }}{% endblock %}
{% if entry.end %}
{% block date_end %}{{ endDatum }}{% endblock %}
{% endif %}
</td>
{% endfor %}
If the entries have the same date I want the <tr> to get the same class, but how do I check if the startDate of Array.1 is the same of Array.2?
As I am not really experienced with twig I can't write much here.
You can't use the function cycle for this use-case.
As you want to keep track of the start_date of a previous entry, you will need to store the date in a (temporary) variable and use that variable to toggle the class when they are not the same date.
{% set is_even = true %}
{% set current_date = null %}
<table>
{% for entry in entries %}
{# store the first start_date in current_date so we can actually compare dates #}
{% if current_date is null %}
{% set current_date = entry.start_date %}
{% endif %}
{# toggle the class when the dates aren't the same #}
{% if current_date|date('U') != entry.start_date|date('U') %}
{% set is_even = not is_even %}
{% set current_date = entry.start_date %}
{% endif %}
<tr class="{{ is_even ? 'even':'odd' }}">
<td class="text-nowrap">
{{ entry.start_date }}
</td>
</tr>
{% endfor %}
</table>
demo
I am looping through an array and would like to add a class if it is the first item.
I have tried
{% if field|first %}
{% if field.first %}
{% if field.first() %}
{% if loop|first %}
{% if loop.first %}
{% if loop.first() %}
none work, please help. (the reason I don't use css pseudo first is that this is a grid layout and I have am doing responsive layout things with it)
{% for field in row.fields %}
<div class="c-product-table__item {% if field|first %}{{ first_item }}{% endif %} ">
<span class="c-product-table__text">{{ field.text }}</span>
</div>
{% endfor %}
I don't know why twig filters are not working, I solved it another way. I set a var and made it blank after first loop
{% set thiscount = 'first_item' %}
{% for field in row.fields %}
<div class="c-product-table__item {{ thiscount }}">
<span class="c-product-table__text">{{ field.text }}</span>
</div>
{% set thiscount = '' %}
{% endfor %}
So I have this loop
{% if employees %}
{% set count = 1 %}
{% set multiplier = 1 %}
{% for employeeIndex, employee in employees %}
<tr>
<td>{{ employee.werknemers }}</td>
{% for workdayIndex, workday in workdays %}
{% if (employeeIndex * multiplier) == workdayIndex %}
<td>x</td>
{% if count == (employees|length) %}
{% set multiplier = multiplier * employees|length %}
{% set count = count + 1 %}
{% endif %}
{% else %}
<td></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endif %}
The result is this :
What I want too happen is for the loop too repeat it self after every employee has a date assigned to them. As u can see the first X begins at 3 and goes down too 13. What I want is the X to begin from 14 again and go down the same way. This should be possible in Twig I believe. Thanks for helping out!!
I did not test this because I can't right now test twig but I think this should work
{% if employees %}
{% set employeeCount = (employees|length) %}
{% for employeeIndex, employee in employees %}
<tr>
<td>{{ employee.werknemers }}</td>
{% for workdayIndex, workday in workdays %}
{% if (workdayIndex - employeeIndex) is divisible by (employeeCount) %}
<td>x</td>
{% else %}
<td></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endif %}
I have a strange situation. My code is :
{% set total_amount=0 %}
{% for result in a_result %}
<tr>
<td>{% set total_amount=total_amount+("%.2f"|format(result.tva*result.prix_ht)) %}
{{ "%.2f"|format(result.tva*result.prix_ht) }}
</td>
/tr>
{% endfor %}
<tr>
<td colspan="5">Total</td>
<td>{{ total_amount }}</td>
</tr>
As result I have :
15.98, 25.49, 25.49
And Total = 65 but total should be equal with 65.96. I don't understand where is the problem. Can you help me please ?
I suggest you to use the round and number_format filter as follow:
{% set total_amount=0 %}
{% for result in a_result %}
{% set value = (result.tva* result.prix_ht)|round(2) %}
{% set total_amount=total_amount+value %}
<tr>
<td>
{{ value|number_format(2, '.', ',') }}
</td>
/tr>
{% endfor %}
<tr>
<td colspan="5">Total</td>
<td>{{ total_amount|number_format(2, '.', ',') }}</td>
</tr>
A running example with sample data in this twigfiddle files
Hope this help
I have this code on my related products:
<h3>HAVE YOU TRIED...</h3>
{% assign max = 10 %}
{% assign count = '' %}
{% assign list = '' %}
{% capture list %},{{ product.id }}{% endcapture %}
{% for collection in product.collections %}
{% if collection.handle contains 'related' %}
{% for product_related in collection.products %}
{% capture id %},{{ product_related.id }}{% endcapture %}
{% unless list contains id %}
{% if count.size < max and product_related.images.size > 0 %}
<div class="rel-product">
<div class="rel-img">
{% for image in product_related.images offset:1 limit:1 %}
<img src="{{ image.src | product_img_url: 'compact' }}}" alt="" />
{% endfor %}
</div>
<div class="rel-cnt">
<h6>{{ product_related.title }}</h6>
<p>{{ variant.option1 }}</p>
<span class="price">{{ product_related.price | money }}</span>
</div>
</div>
{% capture count %}{{ count }}.{% endcapture %}
{% capture list %}{{ list }}{{ id }}{% endcapture %}
{% endif %}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}
It seems to work fine for products in one collection but not at all for products in another collection, the "have you tried" box is empty. Can anyone tell me what this code is doing in terms of picking related products in shopify and why it might not be showing any related products for items in that collection.
Firstly print your 'product' array and check what it will return to you while checking for other one.