[Grok-dev] RFC: In-class template definition in grok.View

kevin at mcweekly.com kevin at mcweekly.com
Mon Apr 30 02:59:57 EDT 2007


Quoting Darryl Cousins <darryl.cousins at treefernwebservices.co.nz>:


>
> shared.py::
>
> def template_factory():
>     template = grok.PageTemplateFile(os.path.join('template.pt'))
>     return template()
>


Hi Darryl,

Thanks for your reply. I played around with your template_factory a  
bit to make it more dynamic.

def template_factory(name):
     template = grok.PageTemplateFile(os.path.join(name))
     return template

Which turned into a TurboGears dottedname inspired version.

from zope.dottedname.resolve import resolve
def template_factory(name):
      return resolve(name)

index = template_factory('myproject.shared.template')

Which then lead to the possibility of extending grok.template().  
Currently grok.template only accepts in-module page templates...

index = grok.PageTemplateFile(os.path.join('index.pt'))

class MyView(grok.View):
     grok.template('index')

But grok.template could be extended to include dotted names...

class MyView(grok.View):
     grok.template('myproject.shared.template')

myproject/shared.py
template = grok.PageTemplateFile(os.path.join('template.pt'))


This allows pretty flexible template re-use across packages and  
modules. I have a rough implementation of this already working if  
there is enough interest.


Kevin



More information about the Grok-dev mailing list