Keats / tera

A template engine for Rust based on Jinja2/Django
http://keats.github.io/tera/
MIT License
3.43k stars 279 forks source link

~ is not allowed in parentheses in Tera 1 #478

Open vandenoever opened 4 years ago

vandenoever commented 4 years ago

In Tera 0.11 an expression like this was possibe:

{%- set tags = (query.tags ~ ' ' ~ tags) | trim -%}

In Tera 1, this gives an error:

Failed to parse "template.html"
  --> 32:30
   |
32 |   {%- set tags = (query.tags ~ ' ' ~ tags) | trim -%}␊
   |                              ^---
   |
   = expected `+`, `-`, `*`, `/`, or `%`\"
vandenoever commented 4 years ago

The keywords and and or are also not allowed in brackets. This makes an expression link this not possible: {% if (a and b) or (c and d) %}.

Now that the function containing has become stricter and will not work on undefined variables, This old expression:

{% if a is containing("a") or b is containing("b") %}

needs a check on the existence of a and b:

{% if a and a is containing("a") or b and b is containing("b") %}

but since or has a higher precedence in Tera this expression needs parentheses.

Keats commented 4 years ago

Ah I didn't think of that when I changed the parentheses handling :/ I'm waiting for pest v3 to rewrite the grammar though since it will have quite a few changes apparently.

kubukoz commented 3 years ago

Bumping, it would be wonderful if and / or were allowed in parentheses to allow explicit / custom precedence.

rootkea commented 3 years ago

The keywords and and or are also not allowed in brackets.

  1. neither are the comparison operators (https://tera.netlify.app/docs/#comparisons)
    {% if (a > b) and (a > c) %}
    {{ "a is highest!" }}
    {% endif %}
  2. nor in operator (https://tera.netlify.app/docs/#in-checking)
    {% if (person in groupA) and (person in groupB) %}
    {{ person ~ " is in both the groups." }}
    {% endif %}

Sometimes parentheses may not be required due to operator associativity and precedence but in general, expressions should be allowed to be grouped in parentheses as it increases the code readability and assures the intended operation flow irrespective of the operator associativity and precedence.

Keats commented 3 years ago

v2 will fix all the operator precedences/parentheses issues and will be the focus after the next release of Zola

Keats commented 3 years ago

(I just checked with the experimental parser I have and all the examples in this thread are parsed correctly)

rootkea commented 3 years ago

Does it also handle these two:

{{ not (not a) }}
{{ not not a }}
Keats commented 3 years ago

Yes

rootkea commented 3 years ago

Great! Looks like we got the parser we needed :) Nice work!