List of tuples in shortcodes

Sometimes it’s useful to give a list to a shortcode. For example:
{{ draw_gallery(images=[“img1.jpg”, “img2.jpg”, “img3.jpg”]) }}

In jinja2, macros can take a list of tuples. For example (in a template):
{% draw_gallery([(“img1.jpg”, “small”), (“img2.jpg”, “big”), (“img3.jpg”, “big”)]) %}

Then in the macro:
{% for image, size in images %}

This doesn’t seem to work with Tera. The Tera docs give the example:

{% for key, value in products %}
{{loop.index}}. {{product.name}}
{% endfor %}

but “key” and “value” are not used in the loop and it’s not clear how they are set in the template. Can a list of tuples be passed to a shortcode in Zola? If so, how? Thanks.

Tuples don’t exist in Tera. The for key, value is used when iterating on serde_json Value::Object (hashmaps/structs basically).

Ok, thanks.

Tuples don’t exist in Tera.

The Tera docs mention “tuple” a few times.
“Specific members of an array or tuple are accessed by using …”
“If you need to sort a list of structs or tuples …”
“Returns true if the given variable can be iterated over in Tera (ie is an array/tuple).”

That’s because Tera uses JSON underneath. So (something, else) in Rust would be transformed into an array since JSON doesn’t have the concept of tuples. Maybe I should remove mentions of tuples in the Tera doc, I can see how that would be confusing.

For your macro example, it might work with nested arrays eg {% draw_gallery([[“img1.jpg”, “small”], [“img2.jpg”, “big”], [“img3.jpg”, “big”]]) %} but I cannot remember if nested arrays are implemented in Tera off the top of my head.

1 Like

Nested arrays do not work. I’ll use another method to accomplish this. Thanks.