On Tue, 2007-04-24 at 10:49 -0700, Jeremy Teale wrote:
> I'm struggling with the template system.
> 
> I have a Display model. This model will render its elements, which are
> of different model types. They are of type Addressbook, Schedule,
> Whiteboard, and so on.
> 
> I'm trying to figure out how I can loop in the Display template and
> call each element's respective template tag to render the snippet.
> 
>  For instance,
> {% for e in elements %}
>        <p>{{ e.call_respective_model_rendering_function }}</p>
> {% endfor %}
> 
> elements is a list of model objects of differing type

The standard object-oriented solution here is that the rendering
function would have the same name for each model. It sounds like you are
getting into difficulties because you need to call a different function
each time. If a group of functions serve exactly the same functional
purpose, give them the same name!

> I've been playing with inclusion tags, but what would be the proper
> way to determine the element's model type and then call the proper
> method?
> 
> Here's an example of one of the inclusion tags I wrote to give a
> better idea of what I've been trying to do.
> 
> @register.inclusion_tag('addressbook/display.html')
> def show_addressbook(addressbook):
>     contacts = addressbook.contact_set.all()
>     return {'name': addressbook.name, 'contacts': contacts}

Since you probably want to use different display templates in each case,
I think the inclusion tag shortcut isn't going to help you here.
Instead, you could create a tag that passed the model instance as a
parameter. Based on the type of that instance, you can then lookup the
right template fragment to populate and the right arguments to use. I
would probably write this so that the template tag function just called
another function based on the type of the argument.

Each of these other functions can then do something like this:

        param_dict = {....}
        template_name = 'addressbook/display.html'
        t = Template(template_name)
        return t.render(Context(param_dict))
        
Of course, you can compact that a lot more if you want to and refactor
it in a number of different ways (e.g. the model-specific functions
return a template name and param dict and the main template tag function
loads the template, renders it to a string and then returns the rendered
result.

Remember, a template tag just returns a string back to the template
framework, so you can happily use things like Template.render(...) to
create those strings. That is all the inclusion tag is doing under the
covers, for example.

Regards,
Malcolm
        


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to