<pre>
<!-- viewing /collections page with collection title of "A", "B", "C", "D" -->
{% assign colletion_titles = collections | map: 'title' %}
{{ colletion_titles }}
</pre>
What output would this liquid be?
The variable "colletion_titles" would be array.
All values will be printed like this:
ABCD
If you want to add commas between the titles, just add a | join: ", "
like this:
{{ colletion_titles | join: ", " }}
Related
I am creating an array in Shopify (Liquid) and I get an error,
{% assign numbers = [
"One",
"TWo",
"three",
"bla"
]
%}
Line 126 — Liquid syntax error: Expected close_square but found comma
in "{{[ "One","TWo", "three","bla" ] }}"
There is no way to create an array like this in liquid.
Instead, you can use the split filter to create an array from a string.
{% assign numbers = "one,two,three,four" | split: "," %}
<pre>{{ numbers | inspect }}</pre>
You can also create an empty array and feed it with the push filter
{% comment %} +++ Creates an empty array +++ {% endcomment %}
{% assign numbers = "" | split: "" %}
<pre>{{ numbers | inspect }}</pre>
{% comment %} +++ Feed the beast +++ {% endcomment %}
{% assign numbers = numbers | push: "one" %}
<pre>{{ numbers | inspect }}</pre>
{% assign numbers = numbers | push: "two" %}
<pre>{{ numbers | inspect }}</pre>
We are trying to solve this issue with an apostrophe in liquid syntax.
We've this value - 'products':'bags' in the variable and we would like to split it and remove the apostrophe.
{% assign values = field | remove: "'" | split: ":" %}
But this is not working.
Any help would be appreciated.
This looks ok to me. When I do
{% assign values = "'products':'bags' " | remove: "'" | split: ":" %}
{% for word in values %}
{{ word }}
{% endfor %}
I get
products
bags
What did you want to be different?
How to group menu by Alphabet in shopify?
I want to group dropdown menu like this.
I am new to shopify, I tried to group menu by trying to get the first letter using this code
{% assign first_letter = {{ link_primary.title | slice: 0 }} %}
but it is giving me following error
Liquid syntax error: Unexpected character { in "{{{{ link_primary.title | slice: 0 }} }}"
You don't actually need the {{ }} around the link title.
You can assign the first letter like:
{% assign first_letter = link_primary.title | slice: 0 %}
Then just check whether it's within a certain range, for example A-D, E-H etc.
I want to be able to apply DRY and not have to repeat myself when building my jinja2 template. So I would like to be able to reference a variable within the jinja2 template from a dynamically constructed variable name, eg:
{% for a in [ 'a', 'b', 'c'] %}
{% set name = a + "_name" %}
{% set value = {{ name }} %}
hello there {{ value }}
{% endfor %}
where my input variables into jinja would be
a_name = 1
b_name = 2
c_name = 3
and I the result would be
hello there 1
hello there 2
hello there 3
is this possible?
I know i would just pass in a datastructure into jinja2 to do something similar, but I am not at liberty to modify what goes into the template.
i got the answer from here
basically, define a contextfunction and reference it within the jinja code:
from jinja2 import Environment, FileSystemLoader, contextfunction
j2_env = Environment( loader=FileSystemLoader(directory), trim_blocks=False )
this_template = j2_env.get_template( """
{% for i in [ 'a', 'b', 'c'] %}
hello there {{ context()[i+"_name"] }}
{% endfor %}
""" )
#contextfunction
def get_context(c):
return c
this_template.globals['context'] = get_context
this_template.render( **{ 'a_name': 1, 'b_name': 2, 'c_name': 3 } )
I need to loop through an array of items, alternating between two different background colors as I go. Is there a way to do this in liquid markup?
Let's say items contains ["a", "b", "c", "d", "e"], then something like this:
{% for item in items %}
{{forloop.index | modulo: 2}} -> {{item}}
{% endfor %}
would produce:
1 -> a
0 -> b
1 -> c
0 -> d
1 -> e