[PHPTAL] Conditional Attribute Output
Werner
lists at mollentze.co.za
Thu Jul 12 14:26:51 CEST 2007
Hi!
I'm having lots of trouble to evaluate conditions inside tal:attributes
statements.
Is it even possible to use conditional expressions inside tal:attributes
statements?
For example, it would be really convenient to only output certain HTML
attributes for an element is certain conditions are met. Consider this
common scenario: You only want to add 'class="last"' to an '<li>' tiem
that is created by using tal:repeat. It's easy to output an unordered
'<ul>' list from a PHP array, but what if you want the last array item
(repeat/item/end) to set a certain class attribute to the last '<li>' item?
So, if you have an array like this...
$arr = array('One', 'Two', 'Three');
and you want this output...
<ul>
<li class="first">One</li>
<li>Two</li>
<li class="last">Three</li>
</ul>
It becomes very difficult, because (and this is the only way I know how
to solve this problem) you have to wrap the LI output in lots of
conditional span tags (with tal:omit-tag) like this:
<ul tal:condition="exists: arr">
<span tal:repeat="item arr" tal:omit-tag="">
<li tal:condition="repeat/item/start" class="first">
<li tal:condition="repeat/item/end" class="last">
</span>
etc...
</ul>
*Please* tell met if there is any way to do something like this (my
imaginary syntax is off course invalid):
<li tal:attributes="class condition:repeat/item/start first; class
condition:repeat/item/end last">
Secondly, please tell me where I can find examples of tal:condition
statements that contain the GT and LT operators - there are not any
examples in the manual as far as I can see?
Thanks for your time and efforts,
Werner
More information about the PHPTAL
mailing list