How to create an array in a for loop in Liquid? - arrays

I'm trying to create an array from a list of objects using Liquid syntax:
{% for operation in menuItems %}
{% assign words1 = operation.Title | split: '_' %}
{% assign controllerName = words1 | first %}
{% assign controllersTmp = controllersTmp | append: '_' | append: controllerName %}
{% endfor %}
I want to split the controllersTmp to get my array, but at this point my controllersTmp is empty.
Any help ?

You can directly create a new empty array controllers and concat to it your controllerName converted into an array using the workaround split:''. The result is directly an array, without the extra string manipulations.
{% assign controllers = '' | split: '' %}
{% for operation in menuItems %}
{% assign controllerName = operation.Title | split: '_' | first | split: '' %}
{% assign controllers = controllers | concat: controllerName %}
{% endfor %}

What worked for me
{% assign otherarticles = "" | split: ',' %}
{% assign software_engineering = "" | split: ',' %}
{% for file in site.static_files %}
{% if file.extname == ".html" %}
{% if file.path contains "software_engineering" %}
{% assign software_engineering = software_engineering | push: file %}
{% else %}
{% assign otherarticles = otherarticles | push: file %}
{% endif %}
{% endif %}
{% endfor %}

you have to init your variable controllersTmp :
{% assign controllersTmp = '' %}

Related

Replace strings from one array with another in Liquid

I need to translate some country names to their denonyms in Liquid, but I'm having a little trouble.
I think I have the underlying logic correct here, however it is only working on products where the "country_of_origin" the last element in each array. So, at the moment a product with country of origin "Australia" will output "Australian", but the transformation doesn't happen on products from any other country.
{% assign countries = "France, Chile, Spain, Australia" | split: ", "%}
{% assign denonyms = "French, Chilean, Spanish, Australian" | split: ", "%}
{% assign d = countries.size | minus:1 %}
{% for i in (0..d) %}
{% assign denonized = product.metafields.custom.country_of_origin.value | replace: countries[i],denonyms[i] %}
{% endfor %}
{{ denonized }}
If anyone were to take a look here I would really appreciate it!
The problem is that you reassign the variable in the loop, so only the last replacement is working.
This is your version but with a correct assignment:
% assign countries = "France, Chile, Spain, Australia" | split: ", "%}
{% assign denonyms = "French, Chilean, Spanish, Australian" | split: ", "%}
{% assign d = countries.size | minus:1 %}
{% assign denonized = product.metafields.custom.country_of_origin.value %}
{% for i in (0..d) %}
{% assign denonized = denonized | replace: countries[i],denonyms[i] %}
{% endfor %}
{{ denonized }}
And this is an example using forloop instead of another variable:
{% assign countries = "France, Chile, Spain, Australia" | split: ", " %}
{% assign denonyms = "French, Chilean, Spanish, Australian" | split: ", " %}
{% assign denonized = product.metafields.custom.country_of_origin.value %}
{% for country in countries %}
{% assign denonized = replaced | replace: country, denonyms[forloop.index0] %}
{% endfor %}
{{ denonized }}
If you have many countries you could add a if previous_value!=new_value break to avoid looping for all the possible countries after you have already replaced.

How do I flatten an array of arrays in shopify liquid?

I've tried to loop through an array of arrays and capture the output using the below:
The array:
["cat1","cat2","cat3","cat4"] ["cat1","cat2"] ["cat4"] ["cat5"]
My attempt:
{% capture newCategories %}
{% for categoryArr in categories %}
{% for category in categoryArr %}
{{category}}
{% endfor %}
{% endfor %}
{% endcapture %}
newCategories --> // new array with items
and it still doesn't give me a flattened array, any ideas?
You should be able to leverage the concat operator: https://shopify.github.io/liquid/filters/concat/
Something like:
{% assign newCategories = '' | split '' %}
{% for categoryArr in categories %}
{% assign newCategories = newCategories | concat: categoryArr %}
{% endfor %}

In Liquid (Shopify) how am I able to get the index position of a specific array object?

In a snippet called 'sb' I have this content
{% assign seller_id = 'another_seller_shop_name,test_seller' | split: ',' %}
{% assign seller_html = 'Another Seller Desc,Seller Description' | split: ',' %}
In the template page - in this case, collection-list I have referenced this snippet
{% include 'sb' %}{% assign seller_id_page = collection.title | replace: ' ','_' | downcase %}
'seller_id_page' will equal to one of the values in 'seller_id'. I just want to be able to return the position of this value, so I can then assign seller_html[x] an index value and render field correctly.
You will need to loop the array and get the index of the corresponding equality.
In code:
{% for item in seller_id %}
{% if item == seller_id_page %}
{% assign position = forloop.index0 %}
{% break %}
{% endif %}
{% endfor %}
{{ seller_html[position] }}
That's the just of it.

Shopify liquid product.thumbnail.liquid repeats block even there is no loops

[![enter image description here][1]][1]My job Is too simple i.e just to add Amazon url in a div block in product thumbnail.liquid.I Have added just simple div, then I found that same div is repeated twice. I have also checked that there is no forloop found still how does it is repeating .Today I found the file called product-loop.liquid which has for loop and product thumbnail.liquid is included. What I need to do if I need to show amazon link block only once? Entire file is in gist link.Thanks.
product-loop.liquid
{% assign product_found = false %}
{% assign skip = false %}
{% assign collection_group = products | map: 'id' %}
{% assign collection_group_thumb = collection_group | append : 'thumb' %}
{% assign collection_group_mobile = collection_group | append : 'mobile' %}
{% capture new_row %}
<br class="clear product_clear" />
{% endcapture %}
<div itemtype="http://schema.org/ItemList" class="products">
{% for product in products limit: limit %}
{% if product.id == skip_product.id or skip == true %}
{% assign product_found = true %}
{% else %}
{% if forloop.rindex0 == 0 and product_found == false and forloop.length != products.count and template != 'search' %}
{% assign skip = true %}
{% else %}
{% include 'product-thumbnail', sidebar: sidebar %}
{% if products_per_row == 2 %}
{% cycle collection_group: '', new_row %}
{% elsif products_per_row == 3 %}
{% cycle collection_group: '', '', new_row %}
{% elsif products_per_row == 4 %}
{% cycle collection_group: '', '', '', new_row %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</div>
may be you including product.thumbnail in a section and that section have loop or conditional logics.

Liquid (Shopify) scope questions

I'm trying to set two variables outside of a few nested loops (for loops) and then check or reassign the variables inside the for. Basically, I'm trying to check for current_tags and display links to any tags under those tags (in the hierarchy I've got). Shopify seems to only support one level of hierarchy (collections and their associated products), but I'm trying to use these tags to make it look like I've got multiple levels of ways of classifying products. So shirt, then red shirt/brown shirt, then red silk shirt/brown silk shirt, as a shitty example.
Here's some code:
{% assign showtag = false %}
{% for link in linklists[settings.main_linklist].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% for link in linklists[link.handle].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% for link in linklists[link.handle].links %}
{% if linklists[link.handle] == empty %}
{% else %}
{% capture temp_tag %}{{current_tags.first | replace: '-', ' '}}{% endcapture %}
link {{link.title}}<br>
temp {{temp_tag}}<br>
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag}
{% assign showtag = true %}
{% endif %}
{% endif %}
{% for link in linklists[link.handle].links %}
{% capture temp_tag %}{{current_tags.first | replace: '-', ' '}}{% endcapture %}
{% if linklists[link.handle] == empty %}
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag %}
{% assign showtag = true %}
{% endif %}
{% else %}
{% if showtag == true %}
<li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
{% endif %}
{% if link.title == temp_tag %}
{% assign showtag = true %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
The above is a little messy but is properly indented on the website. Anyway, it seems like the variable showtag loses scope and cannot be found in the for loops - any solutions?

Resources