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

Kevin Smith kevin at mcweekly.com
Fri Apr 27 16:06:09 EDT 2007

Hi List,

Currently grok.View does not support importing pagetemplates for re-use 
or use pagetemplates defined in-class/in-line.

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

import shared
class MyView(grok.View):
    template = shared.template

This currently raises a "View MyView has no associated template or 
'render' method." As a brittle hack, the following works since if 
self.template is found it is currently called before self.render.

class MyView(grok.View):
    template = shared.template

    def render(self):

To allow in-class template definition, ViewGrokker  would need to check 
factory.template to be a PageTemplate or PageTemplateFile prior to 
raising a 'no associated template or render method'.

Templates can then stay in-module or event in-class. This technique is 
potentially useful for ajax that tends to break views into lots of 
little pieces, for short throw-away templates or to keep examples brief.

class MyView(grok.View):
    """<html metal:use-macro="context/@@master/page">
         <body metal:fill-slot="body">>
          <li tal:repeat="item context/values">
             <a tal:attributes="href python:view.url(item)" 
tal:content="item/title" />
    template = grok.PageTemplate(__doc__)

Please let me know what you think.

Kevin Smith 

More information about the Grok-dev mailing list