[Zope] dynamically generated TAL

N. Thomas nthomas@cise.ufl.edu
Sat, 8 Mar 2003 20:00:47 -0500


* Dieter Maurer <dieter@handshake.de> [2003-03-05 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).
>  > > 
>  > 
>  > 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:
> 
> Because, you did not do the "__of__" magic...

Well, after banging my head on this for a few days, I was finally able
to get this to work. Here is the correct code to do this:

    from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate

    def talgen(self):

        zptstr = \
    """
    <html>
    <title>This is a dynamically generated ZPT page.</title>
    <body>
    This sentence <b tal:omit-tag="">should contain no tags.</b>
    </body>
    </html>
    """ 

        z = ZopePageTemplate(id=None, text=zptstr, content_type="text/html")
        return z.__of__(self)()

The problem earlier was that I was not defining the method with self so
that it would be bound to the context, hence any references later on to
self threw an error.

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

Yup, doing that now. It should be up on zope.org in the New User How-to
section soon.


Much thanks to Dieter Maurer, and hazmat (Kapil Thangavelu) on #zope
(IRC freenode) for helping me out with this.

thomas

-- 
N. Thomas
nthomas@cise.ufl.edu
Etiamsi occiderit me, in ipso sperabo