0

I've got the following card in my drupal installation, I've hidden unnecessary items for my question in the picture.

What I want to achieve is the following: I want the icon to change based on the taxonomy term showed besides the little icon (in this case, test).

enter image description here

This is the code that generates the card:

{% if topic_type %}
  {% embed "node--teaser__field.html.twig" %}     
    {%- block field_icon -%} group {%- endblock -%}
    {%- block field_value -%} {{ topic_type }} {%- endblock -%}
  {% endembed %}
{% endif %}

I think it should look something like this but it gives me an error:

{% if topic_type %}
  {% embed "node--teaser__field.html.twig" %}     
    {% if topic_type == 'test' %}  
      {%- block field_icon -%} icon 1 {%- endblock -%}
    {% else %}
      {%- block field_icon -%} icon 2 {%- endblock -%}
    {% endif%}
    {%- block field_value -%} {{ topic_type }} {%- endblock -%}
  {% endembed %}
{% endif %}

1 Answer 1

0

Twig blocks are named control structures, you can't define them multiple times within the same scope. Either try to move the if statement inside one single block

{% if topic_type %}
  {% embed "node--teaser__field.html.twig" %}     
    {%- block field_icon -%}
      {% if topic_type == 'test' %}
        icon 1 
      {% else %}
        icon 2 
      {% endif%}
    {%- endblock -%}
    {%- block field_value -%} {{ topic_type }} {%- endblock -%}
  {% endembed %}
{% endif %}

or don't overwrite the block at all, instead use variables and pass them down in the embed statement

{% set custom_icon = (topic_type == 'test' ? 'icon1' : 'icon2') %}
{% embed "node--teaser__field.html.twig" with {'icon': custom_icon, 'title': topic_type } %}

I'd recommend the first when there are major overwrites in the HTML output, and the 2nd when only strings/variables/placeholders are replaced.

1
  • Step one has gotten me closer as it accepts the if statement now. Thankyou. Now to figure out why it ignores the if and only takes the else.
    – ronald1993
    Commented Nov 20, 2019 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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