Optimizing TAL (Was: Re: [Zope] Passing arguments to a ZPT)

Paul Winkler pw_lists@slinkp.com
Sun, 23 Mar 2003 14:22:09 -0500


On Sun, Mar 23, 2003 at 06:52:33PM +0000, Johan Carlsson [EasyPublisher] wrote:
> At 13:10 2003-03-23 +0000, Johan Carlsson [EasyPublisher] said:
> >At 22:22 2003-03-22 +0100, Max M said:
> >>Johan Carlsson [EasyPublisher] wrote:
> 
> Regarding my concern for speed of TAL/ZPT.
> I read a from evansimpson (on 
> http://www.zope.org/Members/peterbe/ZPTstyleguide):
> 
> """\
> Page Templates will certainly come close to DTML in speed. The only
> advice I can give with respect to speed is to make a local variable for
> any path that you use several times ...
> evensimpson - 2001-06-22
> """
> 
> As I understand it paths are used all the time in TAL.
> For instance in DTML I usually push an object on the current namespace
> to fill in messages:
> 
> <dtml-with Custom>
> <tr><th><dtml-var header1></td><td<dtml-var content1</td></tr>
> <tr><th><dtml-var header2></td><td<dtml-var content2</td></tr>
> <tr><th><dtml-var header3></td><td<dtml-var content3</td></tr>
> </dtml-with>
> 
> Converting this to TAL I get something like:
> 
> <tr><th tal:content="Custom/header1"></td><td 
> tal:content="Custom/content1"></td></tr>
> <tr><th tal:content="Custom/header2"></td><td 
> tal:content="Custom/content2"></td></tr>
> <tr><th tal:content="Custom/header3"></td><td 
> tal:content="Custom/content3"></td></tr>

Almost. You have to specify where Custom is looked for.
E.g. to look for it in the containment heirarchy of your
template:
 
 <tr><th tal:content="container/Custom/header1"></td><td 
 tal:content="container/Custom/content1"></td></tr>
 <tr><th tal:content="container/Custom/header2"></td><td 
 tal:content="container/Custom/content2"></td></tr>
 <tr><th tal:content="container/Custom/header3"></td><td 
 tal:content="container/Custom/content3"></td></tr>
   
> I get the feeling that this will be come less effective that the DTML 
> version?
> The time spent should increase with the number of paths needed to evaluate?

The difference is that in the TAL version, "Custom" must be found
repeatedly, whereas in the DTML version you only look it up once,
in the dtml-with tag. 

> Could this be optimized some how? Or could it be written in a more 
> efficient way?

yes, this is what Evan suggested.

<table tal:define="C container/Custom">
 <tr><th tal:content="C/header1"></td><td 
 tal:content="C/content1"></td></tr>
 <tr><th tal:content="C/header2"></td><td 
 tal:content="C/content2"></td></tr>
 <tr><th tal:content="C/header3"></td><td 
 tal:content="C/content3"></td></tr>
</table> 

Now you only look up the Custom object once and assign it
to a local variable. Accessing local variables is fast.


-- 

Paul Winkler
http://www.slinkp.com