ZPT tal:define scoping with METAL
I have a ZPT page where I need to create two sections that both iterate off of the same query. The query can be long, so I'd strongly prefer to run it once. No problem, I thought - just define it in an outer <div> tag - I've done that on lots of other pages. However, on this page, each of the two "inner" sections are defined as fill-slots for an overall template. Here's a rough idea of the structure: <html metal:use-macro="myTemplate"> <body> <!-- This is the very slow query --> <div tal:define="results here/someQuery"> <div metal:fill-slot="leftside"> <table> <tr tal:repeat="res results"> ... </tr> </table> </div> <div metal:fill-slot="rightside"> <table> <tr tal:repeat="res results"> ... </tr> </table> </div> </div> </body> </html> When this page is displayed, however, I get an error that "results" is not defined, and it points to the line with the first tal:repeat statement. I tried adding the 'global' keyword to the tal:define, but it made no difference. I'm guessing that the full page isn't being evaluated as it appears, but rather each of the slots is evaluated separately. Is that correct? If so, is there an alternative to having to define the "results" twice (once for each slot)? ___/ / __/ / ____/ Ed Leafe http://leafe.com/ http://opentech.leafe.com
Ed Leafe wrote:
I'm guessing that the full page isn't being evaluated as it appears, but rather each of the slots is evaluated separately. Is that correct? If so, is there an alternative to having to define the "results" twice (once for each slot)?
METAL substitution happens before an code is evalutated. So, your div that defines the results is being substituted out. Either move the define to "myTemplate", in your example, or do something like this in the myTemplate: <html metal:define-macro="main"> <metal:x define-slot="results" tal:define="global results python:[]"/> <metal:x define-slot="leftside"/> <metal:x define-slot="rightside"/> </html> Then in your template: <html metal:use-macro="myTemplate"> <body> <!-- This is the very slow query --> <metal:x fill-slot="results" tal:define="global results here/someQuery"/> <table metal:fill-slot="leftside"> <tr tal:repeat="res results"> ... </tr> </table> <table metal:fill-slot="rightside"> <tr tal:repeat="res results"> ... </tr> </table> </body> </html> cheers, Chris
On Friday, September 5, 2003, at 10:00 AM, Chris Withers wrote:
METAL substitution happens before an code is evalutated. So, your div that defines the results is being substituted out.
Thanks for confirming that.
Either move the define to "myTemplate", in your example, or do something like this in the myTemplate:
Ah! I hadn't thought of adding an additional slot, but that's a lot more elegant than the hack I had come up with. ;-) Thanks. ___/ / __/ / ____/ Ed Leafe http://leafe.com/ http://opentech.leafe.com
Ed Leafe wrote at 2003-9-4 15:57 -0400:
I have a ZPT page where I need to create two sections that both iterate off of the same query. The query can be long, so I'd strongly prefer to run it once. No problem, I thought - just define it in an outer <div> tag - I've done that on lots of other pages.
However, on this page, each of the two "inner" sections are defined as fill-slots for an overall template. Here's a rough idea of the structure:
<html metal:use-macro="myTemplate"> <body>
<!-- This is the very slow query --> <div tal:define="results here/someQuery">
<div metal:fill-slot="leftside"> <table> <tr tal:repeat="res results"> ... </tr> </table> </div>
<div metal:fill-slot="rightside"> <table> <tr tal:repeat="res results"> ... </tr> </table> </div>
</div> </body> </html>
When this page is displayed, however, I get an error that "results" is not defined, and it points to the line with the first tal:repeat statement. I tried adding the 'global' keyword to the tal:define, but it made no difference.
If your "div ... results here/someQuere" is executed (and e.g. not replaced by the macro body), then this will work. There is a common namespace shared by template and macro. Note however, that the complete <XXXX metal:use-macro="..."> ... </XXXX> is replaced by the macro body (with just the fill-slot inserted). Dieter
participants (3)
-
Chris Withers -
Dieter Maurer -
Ed Leafe