-1
{% set switch_check = true %}

{% if switch_check == 'Small' %}
      {{ content.field_value }}
 {% elseif switch_check == 'Large' %}
      {{ content.field_other_value }}
{% endif %}

I want to check if content.switch_check == 'Small' then print content.field_value else if content.switch_check == 'Large' then print content.field_other_value

Am I missing something?

closed as off-topic by leymannx, mradcliffe, kiamlaluno Nov 27 '18 at 6:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – leymannx, kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    If you want to check content.switch_check, why not {% if content.switch_check %}? Your current code will never work as you want it to, switch_check will always be true, never 'Small' or 'Large', because you've declared it as such – Clive Nov 19 '18 at 20:16
  • 1
    I'd consider questions about Twig in general as off-topic TBH. Twig is nothing Drupal-specific. – leymannx Nov 19 '18 at 20:59
  • @Clive Thanks for the quick response. {% if content.switch_check %} this is working and being true on both cases when it is 'Small' or 'Large' – Regokonda Nov 19 '18 at 21:36
  • That only checks the "truthiness" of the value stored in content.switch_check. You might need to read up on Boolean values -- if just evaluates an expression and executes code if the expression evaluates to true. – hotwebmatter Nov 19 '18 at 22:05
1

Yes, you are missing something.

You're setting a value to true and then checking whether it's 'Small' or 'Large'.

This will never return true, as seen below:

{% if true == 'Small' %}
      {{ content.field_value }}
 {% elseif true == 'Large' %}
      {{ content.field_other_value }}
{% endif %}

You may as well check whether 1 == 0 or true == false.

EDIT: Also, as Clive says in his comment above:

If you want to check content.switch_check, why not {% if content.switch_check %}?

In other words, you're not even checking the thing you say you want to check.

In your own words:

I want to check if content.switch_check == 'Small' then print content.field_value else if content.switch_check == 'Large' then print content.field_other_value

So try doing it like this:

{% if content.switch_check == 'Small' %}
      {{ content.field_value }}
  {% elseif content.switch_check == 'Large' %}
      {{ content.field_other_value }}
{% endif %}

Just for sanity's sake, try printing out the value of content.switch_check in a debugging statement so that you know what to expect when you make this comparison! You can remove the debugging statement once you know what the expected value is.

  • 1
    Hi Matt, Thanks for this detailed answer, it really helped in understanding the missing link, I used the content.switch['0']['#markup'] and was able to check successfully. – Regokonda Nov 20 '18 at 7:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.