Adding a Variable in For Loop Liquid - shopify

Is it possible to assign a variable and use that variable in the for loop statement?
I'm trying to place a collection of products in a blog without having to create a new for loop for each article that needs a collection.
What I've done is using the article's tags and splitting just before the collection handle so I can inject it in the for loop, but it's not quite working dynamically yet...
Here's what I've done.. I added in the article tags
collection_some-page-handle
Then in the article.liquid
{% for tag in article.tags %}
{% assign tagMap = tag | split: '_' %}
{% if tagMap.first == 'collection' %}
{% assign collectionName = tagMap.last %}
{% endif %}
{% endfor %}
{{collectionName}} <!-- this shows the handle of the collection -->
{% for product in collections.collectionName.products limit: 8%}
<div class="productWrap">
<img src="{{ product.featured_image | product_img_url: 'medium' }}" alt="">
<p class="product_title" style="border-top:1px solid #efefef;">{{ product.title | split: ' - ' | first }}</p>
</div>
{%endfor%}
Now if i try to put the variable in the for loop it doesnt work, but of course if i put the actual handle it works. Is there a way to do it dynamically?

Instead of using collections.collectionName use collections[collectionName].
This should fix your issue if your variable has the proper handle indeed.
To clarify when you use collections.collectionName you say - "Get me a collection that have handle collectionName`.
When you use collections[collectionName] you say - "Get me a collection using the variable collectionName as a handle."

Related

Iterate lists/content in block template twig Drupal 8

How would I be able to supersede the hierarchical dependencies in Drupal 8's twig engine to be able to loop within the i.e Lists/Views which is assigned to a block. So we would have a template: block--views-block--[machine-name]-1.html.twig You will be required to have the variable {{ content }}
Which then recursively buries itself down to field templates. Its completely killing me that one would need so many levels to produce on block of content.
I would like to iterate within the top custom block template the list.
Attempted
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
To evaluate what is available to iterate down into the object but with no luck. I did though find a nice overriding object structure to reach the field attributes but that was within the field level
item.content['#item'].entity.uri.value
Thanks
i use this to "generate" a picture from my
node--news--full.html.twig
<div class="col-md-3">
{{ content.field_newsbild }}
</div>
the twig debug suggests some filenames. i took this:
field--node--field-newsbild--news.html.twig
and in there i wrote:
{% for item in items %}
<img alt="" src="{{ file_url(item.content['#item'].entity.uri.value) }}" class="img-responsive" {{ attributes }} >
{% endfor %}
hope i'll help a bit.

Filter out collection name from product tag list (display) shopify

I would love to know how to remove from display tags that are used to automatically create collections (they appear in other product section).
For example:
$125
COLLECTION : BUSINESS
TAGS: AUTUMN BUSINESS BLACK
So that BUSINESS tag (which is used to form a BUSINESS collection ) is filtered out.
Tries the following 'straightforward' solution:
{% for tag in product.tags %}
{% unless tag == 'Business' or tag == 'Vintage' or tag == or tag == 'Boho' %}
display {{ tag }} link
{% endunless %}
{% endfor %}
Which did not see to Work
Thanks
Sometimes multiple conditions in if statements don't work so well in liquid. (See here and here.)
You could try something like this instead:
{% assign excluded_tags = "Business,Vintage,Boho" | split: "," %}
{% for tag in product.tags %}
{% unless excluded_tags contains tag %}
{{ tag }}
{% endunless %}
{% endfor %}
EDIT:
I just tested your code again, and even with the typo or tag == or..., it still works fine for me (just a warning on save).
Another suggestion is perhaps it's got to do with capitalisation. E.g. If you have a tag BUSINESS, then tag == 'Business' is not going to work. The capitalisation needs to be consistent.
EDIT 2:
From comment below:
I was thinking how to automatically populate excluded tags by the names of the collections of a product.
You can use map to get an array of collection titles:
{% assign excluded_tags = product.collections | map: 'title' %}
Your straightforward is incomplete.
{% for tag in product.tags %}
{% unless tag == 'Business' or tag == 'Vintage' or tag == <missing_value_here> or tag == 'Boho' %}
display {{ tag }} link
{% endunless %}
{% endfor %}
The third condition is empty hence could be the reason it is failing.
Also as mentioned in another answer by Steph, multiple conditions may be unstable at times, if that the case try this:
{% for tag in product.tags %}
{% unless tag == collection.title %}
display {{ tag }} link
{% endif %}
{% endfor %}
It will take care of any future collections as well.
P.S. This is assuming that the page where the tags are displaying contains a collection url.

Shopify link to tag on home page

Hi Im trying to include links to a collections tags on the home page of my site using link_to_tag. this works fine when on a collection page but doesnt seem to work on the home page instead of giving the url collection/tag-handle it gives homepage/?constraint=tag-handle
any idea how I can achieve this? would rather not require people to have to create a collection for each tag and link to that
brief summary of how I am approaching this:
{% if link.type == 'collection_link' %}
{% assign collection = link.object %}
<ul class="site-nav__dropdown">
{% for tag in collection.all_tags %}
<li>
{{ tag | link_to_tag: tag }}
</li>
{% endfor %}
</ul>
{% endif %}
thanks
If you have the collection name, you can place the collection in anywhere of store.
I used this:
{% assign collection = collections['your-collection-handle'] %}
<ul class="">
{% for tag in collection.all_tags %}
<li>
{{ tag }}
</li>
{% endfor %}
</ul>
I came up with a kind of solution that seems to work for collection pages and non collection pages..... but it seems quite hacky. and I dont know if there are any downsides to this?
{{tag}}

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.

Retrieve all Shopify collections matching specific tag

I'm trying to retrieve all Shopify collections for our store which have products matching tag dog.
{% for collection in collections %}
{% assign gato = 'false' %}
{% assign perro = 'false' %}
{% for tag in collection.tags %}
{% if tag == 'Cat' %}
{% assign cat = 'true' %}
{% elsif tag == 'Dog' %}
{% assign dog = 'true' %}
{% endif %}
{% endfor %}
{% if dog == 'true' and cat == 'false' %}
<li>{{ collection.title | link_to: collection.url }}</li>
{% endif %}
{% endfor %}
I successfully get this list when I'm at homepage (telepienso.com). (See footer screenshot: A). I have the same exact code in collection.liquid and I get some of the collections but NOT all of them. (telepienso.com/collections/all). (See list on the right screenshot: B). Is there any restriction inside collection.liquid which can affect?
A screenshot (productos para perros list):
B screenshot (sección perros list):
This was the cause of the problem. The collections variable was being paginated.
By moving the code in the question outside of the paginate liquid tag, all collections are displayed in the sidebar (the same as the footer).
EDIT: The above link is broken because the question was deleted due to low traffic. I've copied the content from the question below for reference.
Shopify - issue accessing the collections global variable inside paginate
I would like to be able to access the collections global variable from within a paginated group of products, but the collections variable is also paginated if it is accessed within a paginate liquid tag.
For example (in collection.liquid):
{% for collection in collections %}
{{ collection.title }}
{% endfor %}
<br />
{% paginate collection.products by 4 %}
{% for collection in collections %}
{{ collection.title }}
{% endfor %}
...
{% endpaginate %}
Output:
All Collection1 Collection2 Collection3 Collection4 Collection5 Collection6 Frontpage
All Collection1 Collection2 Collection3
The for loop before the paginate tag lists all collections as you would expect, but doing the same thing within the paginate tag causes collections to be paginated as well as the products I am actually wanting to paginate.
Is there a way to access the collections global variable within a paginated group of products without it also being affected by the pagination?
Why would I want to do this? It was causing this problem, and was not immediately obvious because the code using the collections variable was in a separate snippet to the code with the pagination.
EDIT 2: I can no longer reproduce this issue, it appears to have been fixed.

Resources