Conditional class with Twig on div element

10,371

Is that not as simple as:

<div class="{{ divContent=='' ? 'emptyClass' :'notEmptyClass' }}">
 {{divContent}}
</div>
Share:
10,371
webmaster pf
Author by

webmaster pf

Front-end developper on Drupal (6 &amp; 7)

Updated on June 17, 2022

Comments

  • webmaster pf
    webmaster pf about 2 years

    Can I change class added to a div element depending on the content of this div ? Like: "if the div is empty, add this class, else add this class" and targeting the div with an id or something else.

    If yes, how can I do that ? Thanks

    EDIT: I found a workaroud like this

    {% set vueBlocArchives_classes = [
    'liste-archives',
    'type-' ~ node.bundle|clean_class
    ]%}
    {% set vuebloc_classes = [
    'liste-vdl',
    'type-' ~ node.bundle|clean_class
    ]%}
    {% set vueBlocInfoAdmin_classes = [
    'liste-info-admin',
    'type-' ~ node.bundle|clean_class
    ]%}
    

    then I do:

      {% if node.field_inserer_liste_viewfield is not empty and node.id == 77 %}
          <aside {{ vuebloc_attribute.addClass(vueBlocArchives_classes) }}>
            {{ content.field_inserer_liste_viewfield.0 }}
          </aside>
          {# Change classe selon le Nid - Ici nid de la page Info Admin #}
        {% elseif node.field_inserer_liste_viewfield is not empty and node.id == 125 %}
          <aside {{ vuebloc_attribute.addClass(vueBlocInfoAdmin_classes) }}>
            {{ content.field_inserer_liste_viewfield.0 }}
          </aside>
        {% else %}
          <aside {{ vuebloc_attribute.addClass(vuebloc_classes) }}>
            {{ content.field_inserer_liste_viewfield.0 }}
          </aside>
        {% endif %}
    

    But I need to repeat the code so I thought about putting in a variable this part:

     {{ content.field_inserer_liste_viewfield.0 }}
              </aside>
    

    @sakhunzai tip can be a better way, but how can I use it with my code ?

    EDIT2: test to put static code in a variable but syntax errored

      {% set finAside = content.field_inserer_liste_viewfield.0 | render | trim "</aside>" %}
        {# Change classe selon Nid - Ici nid de la page archives #}
        {% if node.field_inserer_liste_viewfield is not empty and node.id == 77 %}
          <aside {{ vuebloc_attribute.addClass(vueBlocArchives_classes) }}>
            {{ finAside }}
    

    Instead this work:

    {% if node.field_inserer_liste_viewfield is not empty %}
          {# Change classe selon Nid - Ici nid de la page archives #}
          {% if node.field_inserer_liste_viewfield is not empty and node.id == 77 %}
            <aside {{ vuebloc_attribute.addClass(vueBlocArchives_classes) }}>
    
              {# Change classe selon le Nid - Ici nid de la page Info Admin #}
            {% elseif node.field_inserer_liste_viewfield is not empty and node.id == 125 %}
              <aside {{ vuebloc_attribute.addClass(vueBlocInfoAdmin_classes) }}>
    
              {% else %}
                <aside {{ vuebloc_attribute.addClass(vuebloc_classes) }}>
                {% endif %}
                {{ content.field_inserer_liste_viewfield.0 }}
              </aside>
            {% endif %}
    

    Inspired by https://stackoverflow.com/a/42144678/2416915 I posted another question similar of these : Is it possible to use Twig addClass with condition?

  • DarkBee
    DarkBee over 4 years
    Not if OP wanted to use drupal's method of adding classes. See here
  • sakhunzai
    sakhunzai over 4 years
    I see even then it will still work, but using drupal conventions will be good though, Thanks for the link
  • webmaster pf
    webmaster pf over 4 years
    @DarkBee Your link doesn't give example about what I want to do ,not ?
  • DarkBee
    DarkBee over 4 years
    @webmasterpf of course not, it was a resource for sakhunzai
  • webmaster pf
    webmaster pf over 4 years
    misunderstood ;)