[Grok-dev] Plugging new template language

Lennart Regebro regebro at gmail.com
Thu Nov 27 10:36:14 EST 2008

On Wed, Nov 26, 2008 at 16:56, Santiago Videla
<santiago.videla at gmail.com> wrote:
> from jinja2 import Template
> import yaml, simplejson, grok, os
> from grokcore.view.components import GrokTemplate
> from grokcore.view.interfaces import ITemplate, ITemplateFileFactory
> class JSONTemplate(GrokTemplate):
>     grok.implements(ITemplate)
>     def setFromString(self, string):
>         self._template = Template(string)
>     def setFromFilename(self, filename, _prefix=None):
>         file = open(os.path.join(_prefix, filename))
>         self._template = Template(file.read())
>     def render(self, view):
>         jinja_render = self._template.render(**self.getNamespace(view))
>         yaml_loader = yaml.load(jinja_render)
>         return simplejson.dumps(yaml_loader)
> I'm doing this because I want to build an application that only use JSON and
> some JavaScript framework on the client side (extjs by now). And I didn't
> like the way I should use grok.JSON (too much classes and methods).
> The question is: is there any way to reload the plugged templates without
> restart zope? like grok does with default ZPT

Sure, you can save the filename and it's file modification time as
attributes in setFromFilename, and then for each render see if the
file modification time has changed, and in that case reload.

ZPT does this automatically in debug mode, I guess Jinja2 does not.

Lennart Regebro: Zope and Plone consulting.
+33 661 58 14 64

More information about the Grok-dev mailing list