问题
In Twig, it's easy to add dynamic classes to elements. But instead of duplicate rules settings , I wonder if I could use condition on element type to use the same "set" array like this:
{% set champ5_classes = [
'label-' ~ node.field_liens_vdl.fieldDefinition.name | replace({"_" : "-"}),
***if it's a UL, add this class :*** 'liste-' ~ node.field_liens_vdl.fieldDefinition.name | replace({"_" : "-"}),
]%}
and then apply it as usual:
<ul {{ champ5_attribute.addClass(champ5_classes) }}></ul>
Perhaps it could be done better at the element level ? Thanks for tips.
EDIT: after reflexion, Idea was to display a div if content exist, and addClass if necessary.Not far of my actual code.I think breakpoint and media queries can fix it more than twig or mix twig and media queries ?
来源:https://stackoverflow.com/questions/59289461/is-it-possible-to-use-twig-addclass-with-condition