[Grok-dev] template association - suboptimal magic?

Jan-Wijbrand Kolman janwijbrand at gmail.com
Thu Sep 11 04:00:23 EDT 2008


Martijn Faassen wrote:
> Jan-Wijbrand Kolman wrote:
> 
>> My sugestion is basically to make the template association simpler by
>> making it more explicit and use less magic. Let try to sketch out the
>> "rules":
>>
>> * The grok.template() directive can be used on a (view) component.
>>
>> * By absence of the grok.template(...) directive, the component's
>> grokker decides what to do. Most likely is to require the component to
>> have a render() method instead.
> 
> I'd say for backwards compatibility's sake alone we'd need to generate 
> the template name based on the class name. It's a bit weird also to 
> require the implementation of render() if and only if a directive is not 
> there. The general idea about directives is to have some default 
> behavior as much as possible by default. Requiring grok.template() if 
> you want to use a template at all means a lot of 'grok.template()' 
> everywhere, which I hope can be avoided.
>
> I'd rather have it pick up 'render' when it is there by itself.

I get your point and in general I agree. In this particular case though:

  class SomeView(grok.View):
    grok.template('someview')
    ...

is equivalent to:

  class SomeView(grok.View):
    grok.template()
    ...

would be equivalent to:

  class SomeView(grok.View):
    ...

*except* that in the first and second case the view component is *not*
allowed to have a render() method where in the last case it is allowed.

I was looking for a way to make this difference more
explicit/predictable for the app. developer, indeed at the "cost" of
possibly having more grok.template() directives throughout the app's
codebase. It could be we have a different idea of how "high" this "cost"
actually is in practice.

There's a bit of a difficulty implementation-wise too (although that
should not be the primary deciding factor in this discussion of course):

The default of a directive - i.e. when the directive is not explicitly
used on a component - cannot be computed. One can only have computed
defaults on directive-binding time (i.e. in the grokker). By then it is
very difficult to decide what classname to use for finding a template -
is it the classname of the view component being grokked? Or maybe of one
of its possible baseclasses?

>> There's another thing that I didn't cover: Although not essential it is
>> really nice to report about templates that are not used at all. This
>> helps the developer keep his codebase clean. I think the
>> grok.templates() directive could keep a global list of templates
>> filenames and check, late in the grokking process, whether all template
>> files that can be found for the application actually are used at least once.
> 
> Do you mean the 'grok.template' directive (without 's')?

Yes.

> We shouldn't be afraid of doing large changes. I think with the 
> modification I gave above, we can make this largely backwards 
> compatible. Since it also sounds like things will be simplified, that's 
> good.

OK - I'll continue on the branch I created for this.

regards,
jw




More information about the Grok-dev mailing list