Andreas Stuhlmüller wrote:
> Hi Adrian,
>
>
>>In your case, pass ITEM_NAMES[item.type]
>>to the template context.
>
>
> Easier said than done. The template was intended to do something like
> this:
>
> {% for item in items %}
> <li>{{ ITEM_NAMES[item.type] }}</li>
> {% endfor %}
>
> Is it possible to achieve this without passing an additional item_types
> list to the context?
>
> Andreas
>
>
Make a template tag.
http://www.djangoproject.com/documentation/templates_python/ , the
section 'Writing custom template tags'.
In a template tag library, you need to create a node class, a compile
function, and then register the compile function. Then use the load tag
to load up your template tag library.
----
Alternatively, the easy way to do this would be to use
template_decorators.py from the new-admin branch at
http://code.djangoproject.com/browser/django/branches/new-admin/django/core/template_decorators.py
Then in your case you can do:
@simple_tag
def lookup_name(names, item):
return names[item.type]
and in your template:
{% load my_template_library %}
{% lookup_name ITEM_NAMES item %}
Of course if ITEM_NAMES is a constant you could import it in the tag
library and not pass it as an argument.
I'll submit template_decorators.py as a separate ticket.