Assigning multiple classes to a tag using PHPTAL
-
20-06-2021 - |
Question
I have an array of data I want to output as a UL using PHPTAL (easy) with class attributes supplied by the array (easy), a class attribute for first and for the last element (easy)... all at the same time (hard).
Ie. I want to combine:
<ul tal:repeat="item items">
<li class="${item/class}">${item/text}</li>
</ul>
with this
<ul tal:repeat="item items">
<li tal:attributes="class repeat/item/first 'first'">${item/text}</li>
</ul>
and this
<ul tal:repeat="item items">
<li tal:attributes="class repeat/item/last 'last'">${item/text}</li>
</ul>
This is purely presentational stuff, so I'd rather do it purely in PHPTAL. Is this possible? How?
La solution
No, there's no pure TALES for this.
<li tal:attributes="class php:repeat.item.last ? 'last'
: (repeat.item.first ? 'first' : NULL)">
Autres conseils
This is a pretty old thread, but because no one mentioned it: an 'cleaner' way is probably through a custom modifier. (http://phptal.org/manual/en/split/custom-modifiers.html). Then you could have:
<li tal:attributes="class css-ordinal:repeat.item">
and as a benefit, you could reuse that in other elements, as it seems a pretty general idiom.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow