[Grok-dev] Re: SVN: grok/branches/regebro-guido-templates/src/grok/
Template types can now be registered.
Philipp von Weitershausen
philipp at weitershausen.de
Tue Oct 2 09:57:20 EDT 2007
Lennart Regebro wrote:
> Log message for revision 80472:
> Template types can now be registered.
>
>
> Changed:
> U grok/branches/regebro-guido-templates/src/grok/_grok.py
> U grok/branches/regebro-guido-templates/src/grok/components.py
> U grok/branches/regebro-guido-templates/src/grok/interfaces.py
> U grok/branches/regebro-guido-templates/src/grok/templatereg.py
> U grok/branches/regebro-guido-templates/src/grok/tests/view/dirandinlinetemplate.py
Neat. May I suggest a slight simplification?
> class PageTemplateFile(GrokPageTemplate, TrustedAppPT,
> pagetemplatefile.PageTemplateFile):
> +
> + interface.implements(interfaces.ITemplateFile)
If you add
interface.classProvides(interfaces.ITemplateFactory)
to this class (note the difference between *provide* and *implement*!),
then the following definition can go away completely:
> +
> +class PageTemplateFileFactory(grok.GlobalUtility):
> +
> + grok.implements(grok.interfaces.ITemplateFactory)
> + grok.name('pt')
> +
> +
> + def __call__(self, filename, _prefix=None):
> + return grok.components.PageTemplateFile(filename, _prefix)
> +
Instead, you would say:
grok.global_utility(grok.components.PageTemplateFile, name='pt')
Basically, what we're doing is letting the *class* provide the factory
interface. Classes are obviously callable, so they fulfill that
ITemplateFactory interface quite well.
It is quite a common pattern to have things like
class Foo:
classProvides(IFooFactory)
implements(IFoo)
We have this for example with vocabularies.
--
http://worldcookery.com -- Professional Zope documentation and training
More information about the Grok-dev
mailing list