When looping through my form errors in Twig:
{% for error in form.vars.errors %}
{{ dump(error) }}
{% endfor %}
How can I determine the validation groups of each constraint that is failing?
Figured this out shortly after posting, I needed error.cause.constraint.groups. Like so:
{% for error in form.vars.errors %}
{% for group in error.cause.constraint.groups %}
{{ group }}
{% endfor %}
{% endfor %}
Related
I have two for loops looking at blocks within a section and I am using an if statement to check the block type each time. I want to show a hr to separate each block of each type, unless its the last block where I don't want the hr to show.
I thought using forloop.last would work but for some reason, the forloop.last check is only working on the second loop. It seems the initial loop is not closing or the forloop.last is not picking up on the if statement for block type. Example code below:
{% if section.settings.productaccordion_active %}
<ul class="product-accordion product-accordion-small">
{% if section.settings.accordion_description_active %}
<li id="product-accordion-1">
<div class="product-accordion-title">Description</div>
<div class="product-accordion-content">
{%- for block in section.blocks -%}
{% if block.type == "accordion_description" %}
{% if block.settings.accordion_description_title %}
<h5>{{ block.settings.accordion_description_title }}</h5>
{{ block.settings.accordion_description_content }}
{% endif %}
{% unless forloop.last %}
<hr>
{% endunless %}
{% endif %}
{%- endfor -%}
</div>
</li>
{% endif %}
{% if section.settings.accordion_ingredients_active %}
<li id="product-accordion-2">
<div class="product-accordion-title">Ingredients</div>
<div class="product-accordion-content">
{%- for block in section.blocks -%}
{% if block.type == "accordion_ingredients" %}
{% if block.settings.accordion_ingredients_title %}
<h5>{{ block.settings.accordion_ingredients_title }}</h5>
{{ block.settings.accordion_ingredients_content }}
<br><span class="product-popup-link" data-popupid="popup_ingredient_A_breakdown">See breakdown</span>
<br><span class="product-popup-link" data-popupid="popup_ingredient_A_full">See full list</span>
{% endif %}
{% unless forloop.last %}
<hr>
{% endunless %}
{% endif %}
{%- endfor -%}
</div>
</li>
{% endif %}
Any help would be greatly appreciated.
The forloop.last condition cannot be fulfilled for each loop because you add an if condition. So if the last item in the loop is not of the type you display, then <hr> will never be displayed.
Here is something that should work (not tested).
First, you create two arrays containing only blocks with needed types.
{% liquid
assign description_blocks = section.blocks | where: "type", "accordion_description"
assign ingredient_blocks = section.blocks | where: "type", "accordion_ingredients"
%}
Then you loop through each array defined above.
This way, for each one, it will always iterate the last item of the loop.
<ul class="product-accordion product-accordion-small">
{% if section.settings.accordion_description_active %}
<li id="product-accordion-1">
{%- for block in description_blocks -%}
Do something
{% unless forloop.last %}
<hr>
{% endunless %}
{%- endfor -%}
</li>
{% endif %}
{% if section.settings.accordion_ingredients_active %}
<li id="product-accordion-2">
{%- for block in ingredient_blocks -%}
Do something
{% unless forloop.last %}
<hr>
{% endunless %}
{%- endfor -%}
</li>
{% endif %}
</ul>
Here is some documentation about the where filter:
https://shopify.github.io/liquid/filters/where/
HTH
I want to display only products from Shopify products but there are showing also pages and posts here is my code for search result:
<div class="product-list">
{% if grid_results == false %}
{% for item in search.results %}
{% include 'search-result' %}
{% endfor %}
{% else %}
<div class="grid-uniform">
{% for item in search.results %}
{% include 'search-result-grid' %}
{% endfor %}
</div>
{% endif %}
{% endif %}
</div>
Now I got the following code:
{% if item.object_type == 'product' %}
But where to put this code so I can get only products for search any text.
Shopify has excellent self help guides for most of the stuff. Here's one such
Show only products in storefront search results
Help to fix my code in phalconphp with volt I think so long
{% for casing in casings %}
{% set i = 0 %}
{% for case in cases if case.casing == casing.type %} //it have 4 loop but cases length is 20
{% set i += 1 %}
{% endfor %}
{% if i == 0 %}
No values set
{% else %}
<ul>
{% for case in cases if case.casing == casing.type %} // it repeat twice in my code
<li> {{ case.description }} - {{ case.value }} </li>
{% endfor %}
<ul>
{% endif %}
{% endif %}
I know have loop.length, loop.index in for loop but when I use if in loop it have not number I want.
{% for casing in casings %}
{% if loop.first %}
<ul>
{% endif %}
{% for case in cases if case.casing == casing.type %}
<li> {{ case.description }} - {{ case.value }} </li>
{% endfor %}
{% if loop.last %}
<ul>
{% endif %}
{% else %}
No values set
{% endfor %}
{% for casing in casings %}
{% if loop.first %}
<ul>
{% endif %}
{% for case in cases if case.casing == casing.type %}
<li> {{ case.description }} - {{ case.value }} </li>
{% endfor %}
{% if loop.last %}
<ul>
{% endif %}
{% else %}
No values set
{% endfor %}
Thankyou for supported
In Jekyll, a post is displayed in its layout thusly:
{{ content }}
Any generators that might have run on that content is displayed like so:
{{ content | toc_generate }}
Unfortunately, this does not work on index pages, as {{ content }} displays nothing on index pages. Instead, we are told to use a for loop:
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
So, the question:
How can I get a generator to run on an index page, since I can't use {{ content }}?
My best guess...
{% for post in site.posts %}
{{ post | toc_generator }}
{% endfor %}
...does nothing.
Any help?
Try this :
{% capture content %}
{% for post in site.posts %}
<h2>{{ post.tile }}</h2>
{{ post.content }}
{% endfor %}
{% endcapture %}
{{ content | toc_generator }}
{{ content }}
I am working with the jekyll static site builder, and I am having
difficulty performing the following:
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
{% for post in site.categories[{{ category }}] %}
<li> {{ post.title }}</li>
{% endfor %}
↩
{% endfor %}
I have a post category in my jekyll site called 'test', and I can
display posts from it with the following:
{% for post in site.categories.test %}
<li> {{ post.title }}</li>
{% endfor %}
However, i want to build an archive page automatically, and in order
to do this, I need to embed the category from the outer loop (the loop
that visits all the categories), and use it inside the inner loop to
access posts from that specific category. What do I need to do to get
the first snippet to work how I want it to?
EDIT: Alternatively, is there another way to get the results that I want?
When you do for category in site.categories ,
category[0] will give you the category name,
category[1] will give you the list of posts for that category.
That's the way Liquid handles iteration over hashes, I believe.
So the code you are looking for is this one:
{% for category in site.categories %}
<h2 id="{{ category[0] }}-ref">{{ category[0] }}</h2>
<ul>
{% for post in category[1] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
<p>↩</p>
{% endfor %}
I've taken the liberty of fixing some markup issues - I've added <ul>...</ul> around the post link list, a <p> around the last link, a semi-colon after the 8617, and also fixed the id at the top (was missing the -ref part).
Regards!
How about...
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
<ul>
{% for post in site.posts %}
{% if post.category == category[0] %}
<li> {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
↩
{% endfor %}
Sure, it's pretty inefficient and generates a bunch of extra whitespace, but it gets the job done.
[The original was missing the tags. Just added them. Also, to get ride of the whitespace, one can collapse everything from for post in site.posts to endfor onto a single line.]
{% for post in site.categories.category %}
- OR -
{% for post in site.categories.category[0] %}
Also, I'm not sure why kshep's example doesn't work...