tyler-sommer / stick

A golang port of the Twig templating engine
MIT License
183 stars 36 forks source link

Test inside a ternary expression breaks parsing #54

Open mjomble opened 1 year ago

mjomble commented 1 year ago

A template like this: {{ x is defined ? 1 : 0 }} results in parse: expected "PRINT_CLOSE", got "PUNCTUATION"

Similarly, this {% set y = x is defined ? 1 : 0 %} results in parse: expected "TAG_CLOSE", got "PUNCTUATION"

PS. There's a chance I'll find the time and motivation to fix this (and other such issues I may run into), so any tips or pointers about the codebase would be welcome 😄

mjomble commented 1 year ago

I managed to look into this a bit.

parseExpr has two steps: parseInnerExpr and parseOuterExpr.

In the {{ x is defined ? 1 : 0 }} example, the inner expression is x and the outer expression is x is defined, but what we need is to go one step further and find the "outer outer" expression x is defined ? 1 : 0.

There are lots of different ways to approach this. @tyler-sommer do you have recommendations?

mjomble commented 1 year ago

PS. A workaround is to change the template like this: {{ (x is defined) ? 1 : 0 }}.