[Grok-dev] should we support a "namespace()" View method

Brandon Craig Rhodes brandon at rhodesmill.org
Thu Oct 11 14:12:05 EDT 2007


Idea: when rendering a View (that lacks a render() method, of course),
the Grok code that hands data to the template should check for whether
the View has a namespace() method.  The rule would be:

 - If a namespace() method exists, it is run, and the object it
   returns is made the root of the template's name space.

 - If no namespace() method exists, then the default is for the
   namespace to be the dictionary:

    { 'context': view.context, 'view': view, 'template': ..., ... }

   and so forth, that contains all the same things we hand ZPT files
   when using Grok today.

This approach would seem to offer easy support for both kinds of
developer: if you want your templates to traverse your raw object
model, just provide an update() method on your View, and go to town on
your "view" and "context" from in your template.  But if you want a
"push" model instead, with all the attractive results that entails,
then write a namespace() method instead of an update() method.

If this is a good idea, then at least two issues have to be decided:

 - Should update() be called if namespace() exists?  It seems to be
   that one would write each View with either an update() or a
   namespace(), but not both, so I am not sure what we should do in
   the presence of both functions.

 - Should namespace() return a dictionary, or a dumb object with
   attributes?  Can we support "pretty" traversal of both kinds of
   data structure?  Having namespace() be a big "return" statement of
   a big dictionary has certain attractions, but not if that means my
   Genshi templates are full of ['xyz'] dictionary attribute
   references instead of simple .xyz attribute references.

-- 
Brandon Craig Rhodes   brandon at rhodesmill.org   http://rhodesmill.org/brandon


More information about the Grok-dev mailing list