tera icon indicating copy to clipboard operation
tera copied to clipboard

Array as a member of an array

Open rootkea opened this issue 4 years ago • 7 comments

Hello!

From https://tera.netlify.app/docs/#literals: "arrays: a comma-separated list of literals and/or idents surrounded by [ and ] (trailing comma allowed)"

Since array is a literal and array is a comma-separated list of literals I was wondering whether it's possible to have array as a member of another array.

This doesn't work:

{% set array = [12, True, 1.7, ["a", 'b', `c`]] %}

But this works:

{% set char_array = ["a", 'b', `c`] %}
{% set array = [12, True, 1.7, char_array] %}

This is inconsistent.

We should either:

  1. Allow array literal to be a member of array just like the way above via variable OR
  2. Update array description at https://tera.netlify.app/docs/#literals as: "arrays: a comma-separated list of literals (except array) and/or idents surrounded by [ and ] (trailing comma allowed)"

I think 1. makes more sense and maintains consistency.

rootkea avatar Jul 06 '21 14:07 rootkea

The v2 parser already handles that correctly so i'll mark 1 as a v2 feature

Keats avatar Jul 06 '21 17:07 Keats

Does v2 parser handle this too? continuing the above example:

{{ char_array in array }}                     // currently, this works
{{ ["a", 'b', `c`] in array }}                // currently, this doesn't work
{{ not ('a' in char_array) }}                 // currently, this doesn't work

Thanks!

rootkea avatar Jan 02 '22 18:01 rootkea

Also, from tera documentation for in operator:

While in the left hand side only literals/variables resulting in a number, a string and a boolean are supported.

But as demonstrated in above example, variables resulting in array are also supported in left hand side. Guess we need to update the docs...

rootkea avatar Jan 02 '22 18:01 rootkea

Hi!

Since this is related to parser I'm not opening a fresh issue.

Does v2 parser handle this:

{% set char_array = ["a", 'b', `c`] %}
{{ char_array != char_array }}                               // Currently, this works
{{ ["a", 'b', `c`] == ["a", 'b', `c`] }}                     // This doesn't work

rootkea avatar Jan 04 '22 10:01 rootkea

I'm not sure right now but it's a good testcase to add it, {{ char_array != char_array }} should be false and the next line should be true

Keats avatar Jan 04 '22 11:01 Keats

First line is indeed false. But unfortunately, Tera throws parsing error on the next line. I hope v2 parser fixes that. :)

rootkea avatar Jan 04 '22 11:01 rootkea

Adding more tests to check with parser v2.

Parentheses with filter:

{{ 1 + ("foo" | length) }}                                        // currently, doesn't work
{{ ("  foo  " | trim) ~ "bar" }}                                  // currently, doesn't work

rootkea avatar Jan 06 '22 08:01 rootkea