[Zope] Using DTMLFile or PageTemplateFile in methods

Peter Bengtsson mail@peterbe.com
Mon, 23 Jul 2001 11:16:49 +0200


> Peter Bengtsson writes:
>  > I use ZPT files for my templates for my Python product development.
>  > This is what I do:
>  >
>  > def manage_addInstance(self,id):
>  >      self._setObject(id, Instance(id))
>  >
>  > class Instance(SimpleItem):
>  >       def __init__(self,id):
>  >              self.id = id
>  >       # in the products folder there is a file called index.zpt
>  >       index_html = PageTemplateFile('index', globals())
>  >
>  > Now, this works, but consider the following extension to the class.
>  >
>  > class Instance(SimpleItem):
>  >       def __init__(self,id):
>  >              self.id = id
>  >       def which_index_html(self):
>  >              return PageTemplateFile('index', globals())
>  >
>  >       index_html = which_index_html
>  >
>  > That does NOT work! It looks like a small change, but I might be wrong.
>  > How to do this anyone?
> The second approach need an additional "call".
>
> ZPublisher is ready to call the "index_html" once.
>
>   In the first case, this calles in fact the PageTemplate
>   and back comes the rendered template.
>
>   In the second case, ZPublisher again calls "index_html"
>   but the result is the PageTemplate itself, and not
>   the rendered result.
>
> Use:
>
> def which_index_html(self):
>     return PageTemplateFile('index', globals())()
>
> i.e. "call" the PageTemplate!
>
>
>
> Dieter