[Grok-dev] RFC: Masterpage implementation without macros
Kevin Smith
kevin at mcweekly.com
Thu Apr 12 14:29:06 EDT 2007
Hi List,
We're using grok for the base of the relaunch of our newspaper website.
I'm not fond of macros, so here is an alternate masterpage
implementation that I think is fairly easy to grok. This sort of
strategy might also apply in a template neutral environment. Please let
me know what you think.
Kevin Smith
Monterey County Weekly
Note: The master template in this case is stuffed in the docstring to
keep the example short.
-- code --
class MasterPage(grok.View):
grok.baseclass()
def _render_master(self):
namespace = self.master.pt_getContext()
namespace['request'] = self.request
namespace['view'] = self
namespace['context'] = self.context
namespace['static'] = self.static # this might need fixing
return self.master.pt_render(namespace)
def content(self):
return super(MasterPage, self).__call__()
def __call__(self):
return self._render_master()
class TestView(MasterPage):
"""
<html>
<head>
<title tal:content="view/title|default">Title</title>
</head>
<body>
<h2 tal:content="view/title|default">Title</h2>
<div tal:content="view/content">
Result of render() method or template
</div>
</body>
</html>
"""
grok.context(interface.Interface)
grok.name('testview')
master = grok.PageTemplate(__doc__)
def title(self):
return "Master Page"
def render(self):
return "Success"
-- result --
<html>
<head>
<title>Master Page</title>
</head>
<body>
<h2>Master Page</h2>
<div>Success</div>
</body>
</html>
More information about the Grok-dev
mailing list