[Zope] dynamically generated TAL not being interpreted

Dieter Maurer dieter@handshake.de
Wed, 5 Mar 2003 20:37:48 +0100


N. Thomas wrote at 2003-3-4 23:18 -0500:
 > * Dieter Maurer <dieter@handshake.de> [2003-03-03 20:56:46 +0100]:
 > > N. Thomas wrote at 2003-3-1 23:46 -0500:
 > > > We have a script that generates TAL. The problem is that the content
 > > > is not being interpeted as TAL by Zope, but rather as raw HTML.
 > > 
 > > You must make a Page Template object from your TAL source code, wrap
 > > it in an appropriate context (--> "__of__") and return it (or call it
 > > to let it render).
 > > 
 > > You can do this only in an External Method (or other filesystem based
 > > code).
 > 
 > Well, this was a start in the right direction. But after wading through
 > the archives, the nearest solution I can cruft together is an external
 > method like this:
 > 
 >     from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
 > 
 >     def talgen():
 > 
 >         zptstr = \
 >     """
 >     <html>
 >     <body>
 >     If properlu rendered,
 >     this sentence <b tal:omit-tag="">should contain no tags.</b>
 >     </body>
 >     </html>
 >     """ 
 > 
 >         zptid = 'foobar'
 >         z = ZopePageTemplate(id=zptid, text=zptstr, content_type="text/html")
 >         return z
 > 
 > But this returns an error, probably because I'm stabbing in the dark.

Because, you did not do the "__of__" magic...

And, please remember, read error messages (Error Type, Error Values *AND*
traceback) carefully and report them, if you do not understand them.

 > Also, I have no idea where exactly to stick the "__of__" method. (I did
 > look at the relevant sections in
 > http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html though.)

Hm, you did not find the "__of__" mentioned in the "Name lookup section"?

You use "return z.__of__(self)"

 > My questions:
 > 
 >     Where is the API for Products.PageTemplates.ZopePageTemplate
 >     documented?

I am not sure, probably in the embedded online help.

*BUT* you need nothing than the constructor (you already used)
and the "__call__" method (that is trivial).

 >     In Products.PageTemplates, should I use PageTemplate, or
 >     PageTemplateFile, or ZopePageTemplate?

As it is not a file in the file system, you do not use "PageTemplateFile" ;-)

Almost surely, "ZopePageTemplate" is right. "PageTemplate" might
work, too.

 >     In the code above, I see that ZopePageTemplate requires that an id
 >     be passed in, does that mean that I have to create a temporary ZPT
 >     object in the Zope filesystem, return that, and somehow make sure it
 >     is destroyed afterwards? I only passed in 'foobar' because it was
 >     required.

No, unless you use special operations (e.g. "template/absolute_url")
what you probably do not do.

 > Also, if anyone could point me to a fragment of code that does something
 > similar it would be much appreciated.

You are very near :-)

I trust you that you will write a HowTo about dynamic generation
of TAL code, once you solved your problem ;-)


Dieter