[PHPTAL] Conditional Attribute Output
Werner
lists at mollentze.co.za
Fri Jul 13 11:07:06 CEST 2007
Thank you so much!
lbedubourg wrote:
> Hi Werner,
>
> Werner wrote:
>> 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?
>>
>
> Yes, but since TALES does not support anything but boolean conditions
> you will have to use the php: modifier.
>
>> 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>
>>
>
> You can create a php function which will return 'first' when the
> iterator is on the first element, 'last' when the iterator is on the
> last element and may be 'odd' or 'even' otherwise.
>
> function li_css_class( $repeat ){
> if ($repeat->start)
> return 'first';
> if ($repeat->end)
> return 'last';
> return null;
> }
>
> And then use it :
>
> <ul tal:condition="exists:arr">
> <li
> tal:repeat="item arr"
> tal:attributes="class php:li_css_class(repeat.item)"
> tal:content="arr/value"
> />
> </ul>
>
> Then you can wrap this function and create a tales modifier :
>
> function phptal_tales_li_css_class( $src, $nothrow ){
> return 'li_css_class(' . phptal_tales_path($src, $nothrow) . ')';
> }
>
> And then :
>
> <ul tal:condition="exists:arr">
> <li
> tal:repeat="item arr"
> tal:attributes="class li-css-class:repeat/item"
> tal:content="arr/value"
> />
> </ul>
>
> This code has not been tested but you get the idea :)
>
>>
>> *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">
>>
>
> You can insert some php there :
>
> <li tal:attributes="class php:repeat.item.start ? 'first' :
> (repeat.item.end ? 'last' : null)">
>
> But it is less readable :)
>
>>
>> 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?
>>
>
> This operators are used with the php: modifier :
>
> <span tal:condition="php:user.emails LT 10">bla</span>
>
> They are just shortcuts to < > <= >= == != use when the XML parser is
> in danger :)
>
> Regards
> Laurent
>
> _______________________________________________
> PHPTAL mailing list
> PHPTAL at lists.motion-twin.com
> http://lists.motion-twin.com/mailman/listinfo/phptal
>
More information about the PHPTAL
mailing list