Colin Howlett wrote:
> Thanks Robert!
> 
> The @inclusion_tag decorator is really nice! Much nicer than what I
> wrote above.
> 
> One thing I'm not too keen on with @inclusion_tag is that the template
> is hard coded in the tag itself. (Did I understand that correctly?)
> That means everytime I want to use a different template, even if the
> logic for populating the context is the same, I need to write a new
> tag. It would be nice if it were possible (optionally) to pass a
> template name as a parameter to an inclusion_tag.
> 
> Looking at the code for inclusion_tag() it doesnt look that hard to
> optionally pass in a template name in the same way you can optionally
> pass in a context - i.e. make file_name an optional parameter and add
> takes_template as an optional parameter defaulting to False. If
> takes_template were set to True the function should take a template
> file name as its first parameter (or second if takes_context is also
> True).
> 
> If nobody objects (rather, if people think it's a good idea), I may try
> to submit a patch for this.
> 

Ok, so I was thinking about something a little different for this issue
(flexibility in the template included).
The filenames would be a varargs, which would be interpolated with the
context you create. So an example would be

@register.inclusion_tag('admin/%(app_label)s/%(model_name)s/whatever',
                        'admin/%(app_label)s/whatever',
                        'admin/whatever')
def whatever(cl):
    return {'app_label': cl.app_label,
            'model_name': cl.model_name,
            'something': cl.something }

So all the names would be interpolated with the context created at
render time, and passed to select_template. This allows similar
flexibility to the top level admin pages.

For now, you can just use @register.simple_tag,

@register.simple_tag
def something(template_name, whatever):
    return render_to_string(template_name, {'something': something}

Also at some point I want to factor out the argument messing from the
rendering, so it would be something like

@register.include_tag('admin/whatever')
@register.takes_context
@register.takes_block
def whatever(context, block, cl):
    return {}

So these things would just add function attribute(s) which would then be
used by gen_compile_func.

I'm not working on this right now, but I think it is worth looking at.

Reply via email to