[PHPTAL] phptales implementation

lbedubourg lbedubourg at motion-twin.com
Fri Feb 16 11:35:47 CET 2007


Richard Standbrook wrote:
> Thanks a lot Laurent, that really helps.
> 
>> Can you post your custom expression code, so we can see where the 
>> problem comes from ?
> 
> Here is my expression function:
> 
> <?php
> 
> function phptal_tales_css_switch($src, $nothrow)
> {
>     $result = phptal_tales_path(trim($src));
> 
>     if($result) {
>         return 'even';
>     }else{
>         return 'odd';
>     }
> }
> 
> ?>
> 

Hi Richard,

First of all, if you are willing to use this modifier inside a loop, you 
can use the repeat/item/odd repeat/item/even as explained in the manual 
to accomplish this :

<tr tal:repeat="row myRows" class="row${repeat/row/even}">
</tr>

Which will produces css classes : row1, row0, row1, row0

Concerning tales expression modifiers,

Your function must produce PHP code which will be included inside the 
template.

Your function is only executed at template parse time and not during 
template execution.

You should have written something like :


function phptal_tales_css_switch($exp, $nothrow=false){
     return PHPTAL_TalesInternal::path($exp,$nothrow).' ? "even" : "odd"';
}

the 'path' method returns the PHP code which evaluates $exp and you just 
have to concatenate it with your own code to extend the system.

The above method will produce the folowing code for $exp = 'repeat/row/even'

phptal_path($ctx->repeat, 'row/even') ? "even" : "odd"

Please note that can always fallback to PHP when you need a solution 
quickly and don't know how to create or do not have the time to create 
the matching tales modifier :

tal:attributes="class php:repeat.row.even ? 'even' : 'odd'"
tal:attributes="class php:myfunction(somevalue)"

And you can do this inside shortcuts :

class="${php:repeat.row.even ? 'even' : 'odd'}"

Regards
Laurent



More information about the PHPTAL mailing list