Hi, I'm trying to use a centralized ZTP which could be called from other tempate/methods. I want to pass arguments to the ZTP to be able to customized it. Short version: How do I access keyword arguments passed to a ZPT? Traditionally (I'm accually converting a DTML Method template to ZPT) I either passed the as keyword arguments <dtml-var "''printview(_.None,_, key1=value1, ...)"> or pushed the on top of the namespace: <dtml-let key1="value1" ...><dtml-var "''printview(_.None,_)"></dtml-let> Now I'm trying to do the same thing: <dtml-var "_.getitem(''printview.pt")(key1=value1, ...)"> or <dtml-let key1="value1" ...><dtml-var printview.pt></dtml-let> but the passed arguments can't be accessed in the ZPT: <p tal:content="key1">Key1</p> or <p tal:content="here/key1">Key1</p> or <p tal:content="request/key1">Key1</p> or (definitely not) <p tal:content="namespace/key1">Key1</p> Is there a way to do this? Regards, Johan Carlsson -- Easy Publisher Developers Team Johan Carlsson johanc@easypublisher.com Mail: Birkagatan 9 SE-113 36 Stockholm Sweden Phone +46-(0)8-32 31 23 Fax +46-(0)8-32 31 83 Mobil +46-(0)70-558 25 24 http://www.easypublisher.com
Johan Carlsson [EasyPublisher] wrote:
Short version: How do I access keyword arguments passed to a ZPT?
Have a look at "options" as access-point to keyword-arguments... Taken from the ZopeBook (http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/AdvZPT.stx): """The action script can do all kinds of things. It can validate input, handle errors, send email, or whatever it needs to do to "get the job done". Here's a sketch of how to validate input with a script: ## Script (Python) "action" ## if not context.validateData(request): # if there's a problem return the form page template # along with an error message return context.formTemplate(error_message='Invalid data') # otherwise return the thanks page return context.responseTemplate() This script validates the form input and returns the form template with an error message if there's a problem. The Script's context variable is equivalent to here in TALES. You can pass Page Templates extra information with keyword arguments. The keyword arguments are available to the template via the options built-in variable. So the form template in this example might include a section like this: <span tal:condition="options/error_message | nothing"> Error: <b tal:content="options/error_message"> Error message goes here. </b></span> This example shows how you can display an error message that is passed to the template via keyword arguments. Notice the use of | nothing to handle the case where no error_message argument has been passed to the template.""" HTH, -mj
Johan Carlsson [EasyPublisher] wrote:
I'm trying to use a centralized ZTP which could be called from other tempate/methods. I want to pass arguments to the ZTP to be able to customized it.
Most likely you should be using metal:template for this kind of work! A template acts like a function, only that it takes complicated ZPT as arguments. It really is the superior way. Please do try it out. It sounds more complicated than it actually is. regards Max M
At 22:22 2003-03-22 +0100, Max M said:
Johan Carlsson [EasyPublisher] wrote:
I'm trying to use a centralized ZTP which could be called from other tempate/methods. I want to pass arguments to the ZTP to be able to customized it.
Most likely you should be using metal:template for this kind of work! A template acts like a function, only that it takes complicated ZPT as arguments. It really is the superior way. Please do try it out. It sounds more complicated than it actually is.
Yes, indeed after converting some of the DTML-methods to metal macros things get a bit easier. Still I have measured a 30% speed penalty between the metal-template and the DTML Methods. One thing that feels very inconsistent is that I have to use metal:use-macro to include a macro instead of using tal:content="dtml_method". I guess I could start using tal:replace with dummy tags, it might be what I should do anyway, to also be able to present a renderable mock-up default. Best Regards, Johan Carlsson -- Easy Publisher Developers Team Johan Carlsson johanc@easypublisher.com Mail: Birkagatan 9 SE-113 36 Stockholm Sweden Phone +46-(0)8-32 31 23 Fax +46-(0)8-32 31 83 Mobil +46-(0)70-558 25 24 http://www.easypublisher.com
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> 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? Could this be optimized some how? Or could it be written in a more efficient way? Or is it even possible to use something similar to dtml-with? Best Regards, Johan Carlsson -- Easy Publisher Developers Team Johan Carlsson johanc@easypublisher.com Mail: Birkagatan 9 SE-113 36 Stockholm Sweden Phone +46-(0)8-32 31 23 Fax +46-(0)8-32 31 83 Mobil +46-(0)70-558 25 24 http://www.easypublisher.com
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
participants (4)
-
Johan Carlsson [EasyPublisher] -
Maik Jablonski -
Max M -
Paul Winkler