Is it possible to use Twig addClass with condition?

此生再无相见时 提交于 2020-01-06 08:06:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!