This question has been addressed within the context of javascript and ruby by this community, but how would you accomplish the same thing using Jekyll / Liquid templating? Hypothetically, something along the lines of:
{% assign subarrays = array | split_items_by: 3 %}
Verbose code :
{% assign subArraySize = 3 %}
{% assign myArray = "one,two,three,four,five,six,seven" | split: "," %}
myArray = {{ myArray | inspect }}
{% assign multiArray = "" | split: "/" %}
multiArray = {{ multiArray | inspect }}
{% for element in myArray %}
looping in myArray - forloop.index = {{ forloop.index }}
{% assign reminder = forloop.index | modulo: subArraySize %}
reminder : {{ reminder | inspect }}
{% if reminder == 1 %}
create a new empty sub array
{% assign subArray = "" | split: "/" %}
subArray = {{ subArray | inspect }}
{% endif %}
push current element in subArray
{% assign subArray = subArray | push: element %}
subArray = {{ subArray | inspect }}
{% if reminder == 0 or forloop.last %}
push subArray in multiArray if subarray length is
{% assign multiArray = multiArray | push: subArray %}
multiArray = {{ multiArray | inspect }}
{% endif %}
{% endfor %}
{% for subArray in multiArray %}
subArray :{{ subArray | inspect }}
{% endfor %}
If you already have ruby code available, you can write a tag plugin
Related
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.
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.
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 = '' %}
I want to list all my posts within site.categories.projects as a comma separated sentence. There's documentation for displaying {{ site.tags }} as array_to_sentence_string but how can I use the filter with a for loop?
# empty array
{% assign postsTitlesArray = '' | split:':' %}
# pushing categorie posts title in our array
{% for post in site.categories.one %}
{% assign postsTitlesArray = postsTitlesArray | push: post.title %}
{% endfor %}
{{ postsTitlesArray | array_to_sentence_string }}
I have a problem with an array of arrays in Twig.
Here is the code I am struggling with :
{% set tabTmp = {0:{},1:{},2:{},3:{},4:{},5:{},6:{},7:{},8:{}} %}
{%for element in box.elements%}
{% set tab = tabTmp[element.category.id] %}
{% set elementId = element.id %}
{% set tab = tab | merge({elementId:element}) %}
{% endfor%}
{%for key, tmp in tabTmp %}
{% if tmp is iterable %}
{{ dump(tmp) }}
{% endif %}
{% endfor%}
box.elements and element exist, element.category.id and element.id are integer and element is the object I want to work with.
But I keep having Array(0) as a result of dump(tmp).
Any ideas ?
Everything looks fine, but if you want to merge a variable as key to an associative array you need to use ();
so try changing
{% set tab = tab | merge({elementId:element}) %}
To
{% set tab = tab | merge({(elementId):element}) %}