Using DTMLFile or PageTemplateFile in methods
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? In my product the templates used will change inside the instance, so that /mysite/instance1 uses one template and /mysite/instance2 uses another. Cheers, Peter
Peter Bengtsson wrote:
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?
Well, it would probably work if you wrote:
index_html = which_index_html()
...but I get the idea that that's not what you want.
In my product the templates used will change inside the instance, so that /mysite/instance1 uses one template and /mysite/instance2 uses another.
It's hard to know what you need to do without more information. Cheers, Evan @ digicool & 4-am
index_html = which_index_html()
can't really do that because I need the "zopeself".
...but I get the idea that that's not what you want.
In my product the templates used will change inside the instance, so
that
/mysite/instance1 uses one template and /mysite/instance2 uses another.
It's hard to know what you need to do without more information.
Current:: index_html = PageTemplateFile('index', globals()) # works What I want:: def index_html(self): " " return PageTemplateFile('index', globals()) # does not work. HTTP 404 instead # return "hello world!" works fine here. Basically, I want the serving of my .zpt files to be dynamic. I don't want to do it just once in the begining when the class is first instanciated. Perhaps this is impossible. I could prepare the templates a bit more manually if that turns out to be needed, but I can't possible handcode the various templates. There's another script that loops the products folder to list all the possible templates called .zpt I have there. Cheers, Peter
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
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
Still not working! It was:: index_html = PageTemplateFile('index', globals()) which was working fine I tried:: def index_html(self): " " return "<html>hello</html>" which also worked. Then I tried:: def index_html(self): " " # 'simple' only contains "<html>hello</html>" return PageTemplateFile('simple', globals()) which caused a NotFound supposedly meaning that the index_html was "broken". I also tried your advice:: def index_html(self): " " # 'simple' only contains "<html>hello</html>" # call, call the PageTemplateFile() so that it returns HTML, not object return PageTemplateFile('simple', globals()) () which gives me this error: Error Type: AttributeError Error Value: 'string' object has no attribute 'other' File C:\Program Files\zope\lib\python\Shared\DC\Scripts\Bindings.py, line 351, in _bindAndExec (Object: simple) File <string>, line 1, in ? File C:\Program Files\zope\lib\python\Shared\DC\Scripts\Bindings.py, line 304, in _getTraverseSubpath (Object: simple) AttributeError: (see above) Bare in mind that 'simple.zpt' contains only <html>hello</html> and works fine the static way. If this is a bug or can't be done easily, I'm going to have to use my .zpt files to create ZPT instances in Zope instead.
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
Peter Bengtsson writes:
I also tried your advice:: def index_html(self): " " # 'simple' only contains "<html>hello</html>" # call, call the PageTemplateFile() so that it returns HTML, not object return PageTemplateFile('simple', globals()) () which gives me this error: Error Type: AttributeError Error Value: 'string' object has no attribute 'other'
File C:\Program Files\zope\lib\python\Shared\DC\Scripts\Bindings.py, line 351, in _bindAndExec (Object: simple) File <string>, line 1, in ? File C:\Program Files\zope\lib\python\Shared\DC\Scripts\Bindings.py, line 304, in _getTraverseSubpath (Object: simple) AttributeError: (see above) The template want to be embedded into the Zope acquisition context:
Try: return PageTemplateFile('simple', globals()).__of__(self) () Dieter
Cheers buddy!! That helped. When I create an entry in ZopeLabs.com I'll mention your name. Peter
The template want to be embedded into the Zope acquisition context:
Try:
return PageTemplateFile('simple', globals()).__of__(self) ()
Dieter
participants (3)
-
Dieter Maurer -
Evan Simpson -
Peter Bengtsson