1

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?

4

2 回答 2

1

不,这没有纯粹的故事。

<li tal:attributes="class php:repeat.item.last ? 'last' 
                          : (repeat.item.first ? 'first' : NULL)">
于 2012-08-28T23:02:40.747 回答
1

这是一个相当古老的线程,但因为没有人提到它:“更清洁”的方式可能是通过自定义修饰符。(http://phptal.org/manual/en/split/custom-modifiers.html)。然后你可以有:

<li tal:attributes="class css-ordinal:repeat.item">

作为一个好处,您可以在其他元素中重用它,因为这似乎是一个非常普遍的习语。

于 2016-02-18T17:22:08.817 回答