[Grok-dev] Re: first thoughts on "regebro-guido-templates"

Lennart Regebro regebro at gmail.com
Sat Nov 3 13:10:56 EDT 2007

So, I checked in some changed to the branch. The changes are:

1. A template directory in tests, testing the template plugin
features. Of course, I later realized that Brandon had done functional
tests for this as well. Having both doesn't seem useful. Which ones
should we keep?

2. GrokPageTemplate is now split in two classes, BaseTemplate, from
which all templates must inherit, and GrokTemplate, which contains all
of the support for the simple page templating story. (I removd the
"page" part of the name to make a clearer distinction from Zope Page

The reason for this was that the ZPT implementation doesn't follow the
simplified story in any way. Most significantly, they inherited from
both ZPTs and the BasePageTemplate, while the simplified story has the
template not as  mixin, but as an attribute.

I have also refactored the ZPT implementation to use the simplified
story, meaning that we don't really need to separate BaseTemplate and
GrokTemplate, but I do feel that it is neater to have a BaseTemplate
which doesn't implement any of the simplified help, if you don't want
it. And also, maybe the ZPT refactoring I did has some weird
drawbacks, and we need to revert it... Although the tests pass.

3. The GlobalUtilityGrokker has had it's priority changed to 1100, so
it gets run before the template grokker, so that _template directory
files with newly supported extensions gets picked up if they are in
the same product as the support for them.

4. The simple story has been further simplified according to the
conclusions after this thread.

You still need to create two classes. A template class and a factory
class. Minimally, they can look like this:

class MyPageTemplate(grok.components.GrokPageTemplate):

    def fromTemplate(self, template):
        return MyTemplate(template)

    def fromFile(self, filename, _prefix=None):
        file = open(os.path.join(_prefix, filename))
        return MyTemplate(file.read())

    def render(self, view):
        return self.getTemplate().render(**self.getNamespace(view))

class MyPageTemplateFactory(grok.GlobalUtility):


    def __call__(self, filename, _prefix=None):
        return MyPageTemplate(filename=filename, _prefix=_prefix)

Thats it!

More information about the Grok-dev mailing list