On Sep 1, 1:05 pm, joune <[EMAIL PROTECTED]> wrote:
> Hi all,
> I'm totally new to Django and i'm starting to play with templates...
> I have a list to display and a separate list of elements considered
> special;
> so i'd like to display the first list with a different style if the
> element is contained in the "specials list"
>
> for instance:
> list = [ 'john', 'bill', 'robert', 'greg' ]
> specials = [ 'bill', 'greg' ]
>
> then i wish i could use in the template:
> {% for name in list %}
> {% if name in specials %} <b>{{ name }}</b>
> {% else %} {{ name }}
> {% endif %}
> {% endfor %}
>
> Unfortunately this 'if' syntax is not supported by the templating
> system.
> Neither can i call a function that would take name as an argument and
> return True or False.
> This seems a very basic use case (much simpler than many advanced
> template features such as 'regroup') yet I can't find the correct way
> to do this, so i believe i must have missed something..
>
> Could someone please help me with this?
> Thanks.. (do not hesitate to ask precisions if my use case is not
> clear..)

You can't do this with the built-in filters, but it's trivially easy
to write a custom filter that will do it. Something like (untested):

from django import template
register = template.Library()

@register.filter
def is_in(value, value_list):
    return value in value_list

and in the template:

{% if name|is_in:specials %}

--
DR.
--~--~---------~--~----~------------~-------~--~----~
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