Saturday, March 12, 2022

[FIXED] How to properly extend twig ternary statement?

Issue

I am trying to include a modal in a new place, and in order to direct a proper controller for a template, I am distinguishing them by different values passed from 3 other templates - which, in my case, are:

{% include '@path' %}
{% include '@path' with { foo: 'a' } %}
{% include '@path' with {foo: 'b'} %}

And these are conditions in the invoked template which I came up with to distinguish which controller should be used right now

Originaly, it was just:

{% set controller = foo | default('') == 'a' ? 'ctrl' : 'ctrl2' %}

and it worked properly.

{% set controller = foo | default('') == 'a' ? 'ctrl' : 'b' ? 'ctrl' : 'ctrl2' %}

But now, after modification I can't manage to make it return the 'ctrl2' value, so modal won't display.

Can you help? Is this even sufficient amount of information to describe this problem? Thank you.


Solution

You forgot one element in your second condition :

{% set controller = foo | default('') == 'a' ? 'ctrl' : (foo | default('') == 'b') ? 'ctrl' : 'ctrl2' %}


Answered By - jean-max

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.