twig.js icon indicating copy to clipboard operation
twig.js copied to clipboard

Still problems with ternary operators in arrays.

Open koskinpark opened this issue 2 years ago • 8 comments

twig.js:^1.15.4

{% set classes = [ 'a-button', modifier ? modifier, ] %}

This returns now { classes: [ 0: undefined ] }

Normally, if modifier is not exist, we should return only ['a-button']. Or if it's exist -> it should return ['a-button', 'some-modifier-value'], or even ['a-button', ['some-modifier-value-1', 'some-modifier-value-2']]

koskinpark avatar Jan 11 '23 08:01 koskinpark

We are trying to simulate drupal's behaviors in twig.js Example of usage in Drupal https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/themes/claro/templates/form-element.html.twig#L26

koskinpark avatar Jan 11 '23 08:01 koskinpark

Can you provide an example of it working as expected in Twigfiddle?

willrowe avatar Jan 13 '23 15:01 willrowe

@willrowe Here is a twigfiddle example, very simplified version of the Drupal example from @koskinpark : https://twigfiddle.com/caakml

sarahjean avatar May 15 '23 18:05 sarahjean

I did a little bit of testing with this and it appears to be an issue in the parsing of the array. If I break out each of the items into a separate print statement, it seems to work properly.

willrowe avatar May 16 '23 14:05 willrowe

I can confirm @willrowe assessment, this is a very annoying parsing issue. Putting the ternary expression between brackets solve the issue.

Not working: {% set classes = [ style_name ? 'image-style-' ~ style_name %}

Working: {% set classes = [ (style_name ? 'image-style-' ~ style_name) ] %}

This is very annoying when simulating existing Drupal templates.

dgervalle avatar May 14 '24 15:05 dgervalle

While annoying, the brackets solution works and shouldn't be an issue for Drupal.

arakwar avatar Jul 08 '24 17:07 arakwar

@arakwar They don't break anything on the Drupal side, you are right, but it is crummy to have to be aware of this to get your stuff working, twig.js should be as close to php twig as it can to be useful. A lot of folks may not find their way here and will probably ditch tools that are using this rather than come to the conclusion that there is a slight library mismatch, since the twig itself is valid syntax.

sarahjean avatar Jul 08 '24 18:07 sarahjean

@sarahjean I 100% agree that this needs to be fixed. My point is that the brackets should not interfere with Drupal, so anyone who wants to use this solution don't have to wait for the fix.

arakwar avatar Jul 08 '24 21:07 arakwar