[Zope] Page Templates define

Tino Wildenhain tino@wildenhain.de
Mon, 28 Oct 2002 01:24:08 +0100


Hi Mark,

you should avoid heavy calculations in a page template.
Better use a python script, which can be aquired, and
call it in the template whereever you need it.
To avoid long running calculations every time if
you call the script multiple times in a template,
you can use the following construct:

if request.has_key('_yourspecialkey'):
    return request['_yourspecialkey')

value=your routine

request.set('_yourspecialkey',value)
return value


Remember, you can construct and return a fairly complex
object Structure, like a list or dictionary or list of
dictionaries (preferred) where you can use simple path
expressions in your template.

HTH
Tino

> Ahh,  Ok.  It all makes sense now.
>
> So to make something *truly* global to a template, and all processing
> done  within that template, I define variables before an processing is
> done.
>
> So, my example below turns into this:
>
>
> <span tal:omit-tag=""
>       tal:define="objs here/regionObjects;
>                num_objs python:objs[0];">
>
> <html metal:use-macro="here/GetTemplate">
>
> ...
>
> </html>
> </span>
>
> This seems a little sloppy.  Is there a better way to do it?
>
> This also leads me to believe that there's no way to define site-wide
> globals  available in every Page Template that's generated.  What if I
> wanted a module  available in every page without having to define it in
> every page?  e.g:
>
> tal:define="global pps modules/Products/PythonScripts/standard"
>
> I can't really get this to work, can I?
>
> Mark
>
>>
>> This is the answer from Tres when I had the same problem:
>>
>>
>> """
>> AFAIK, global names are defined in the scope of the template which
>> includes the macro;  the "source" template for the macro is not itself
>> "evaluated" before inclusion (METAL processing is done *before* TAL
>> processing).
>> """
>>
>> --On Sonntag, 27. Oktober 2002 13:23 -0700 Mark Gibson
>> <mark@dimensional.com> wrote:
>>
>> > I define a variable in a tag, and try and use it in a contained tag,
>> > and get a NameError on that variable.  Can anyone help me understand
>> > why? The snippet of code is below:
>> >
>> > <html metal:use-macro="here/GetTemplate"
>> >        tal:define="objs here/regionObjects;
>> >                 num_objs python:objs[0] ">
>> >
>> > <div metal:fill-slot="body">
>> > <table  border="0" width="100%"
>> >     tal:condition="python:num_objs > 11"
>> >   >
>> > ...
>> >
>> > Error Type: NameError
>> >   Error Value: global name 'num_objs' is not defined
>> >
>> >
>> >
>> >
>> > _______________________________________________
>> > Zope maillist  -  Zope@zope.org
>> > http://lists.zope.org/mailman/listinfo/zope
>> > **   No cross posts or HTML encoding!  **
>> > (Related lists -  http://lists.zope.org/mailman/listinfo/zope-announce
>> >  http://lists.zope.org/mailman/listinfo/zope-dev )
>>
>>
>>
>>
>>     ---------------------------------------------------------------------
>>    -    Andreas Jung                     http://www.andreas-jung.com   -
>>   -   EMail: andreas at andreas-jung.com                              -
>>    -            "Life is too short to (re)write parsers"               -
>>     ---------------------------------------------------------------------
>>
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )