RE: [Zope] Questions about DocumentTemplate.
Mark wrote:
I want DocTemplate to expose an object model, and it seems perfect for the task. However, Im having trouble using "." expressions.
Code speaks louder than words, so my code is: ## class Spam: def eggs(self): return "Eggs"
spam=Spam()
from DocumentTemplate import * ss=HTML('Eggs are <!--#var expr="spam.eggs()"-->') print ss(spam=spam)
Here's an alternative that works: from DocumentTemplate import HTML class Spam: def eggs(self): return "Eggs" ss = HTML('Eggs are <!--#var eggs-->.') s=Spam() print s.ss(s) and a second choice: from DocumentTemplate import HTML class Spam: def eggs(self): return "Eggs" ss = HTML('Eggs are <!--#var eggs-->.') s=Spam() print s.ss(s) --Paul
Here's an alternative that works:
Thanks for the response. My problem is that I want a deep object model. I want the user to be able to say "spam.eggs" or "pantry.eggs" etc. Just using eggs wont do - I really need it qualified. The solution I came up with was: class DotWrapper: def __init__(self, ob): self._obj_ = ob def __getattr__(self, attr): bits = string.split(attr, '.') ob = getattr(self._obj_, bits[0]) for bit in bits[1:]: ob = getattr(ob, bit) return ob Then: s=Spam() print s.ss(DotWrapper(s)) does pretty-much the right thing... Thanks, Mark.
participants (2)
-
Mark Hammond -
Paul Everitt