Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Still problems with ternary operators in arrays. #853

Open
koskinpark opened this issue Jan 11, 2023 · 5 comments
Open

Still problems with ternary operators in arrays. #853

koskinpark opened this issue Jan 11, 2023 · 5 comments

Comments

@koskinpark
Copy link

koskinpark commented Jan 11, 2023

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
Copy link
Author

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

@willrowe
Copy link
Collaborator

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

@sarahjean
Copy link

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

@willrowe
Copy link
Collaborator

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.

@dgervalle
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants