views:

105

answers:

3

Hello

How can I achieve this using the Django template system:

Say I have 2 variable passed to the template system:

days=[1,2,3,4,5]
items=[ {name:"apple,day:3},{name:"orange,day:5} ]

I want to have such output as a table:

        1   2    3    4    5
apple   n   n    y    n    n
orange  n   n    n    n    y

As you can notice, giving "n" to non matching ones and "y" to matching.

+2  A: 

Two loops. The outer loop is through items, the inner through days. Test if outer[day] is equal to inner, and output y if so and n if not.

Ignacio Vazquez-Abrams
cant do this with django 1.1 :(
Hellnar
That makes no sense. It's two for tags and a ifequal tag.
Ignacio Vazquez-Abrams
Of course you can do it, see my answer.
Daniel Roseman
+6  A: 

Why don't you define this logic in the django view, and then simply pass arrays of Ys and Ns to the template?

Daniel Vassallo
+1 This is exactly what I would do.
hughdbrown
George
Do the least you can do in the templates.
S.Lott
+6  A: 

Here's what Ignacio meant. That said, I probably agree with Daniel that you should do this in the view.

<table>
{% for item in items %}
  <tr>
    <td>{% item.name %}</td>
    {% for dday in days %}
    <td>
      {% ifequal dday item.day %}y{% else %}n{% endifequal %}
    </td>
    {% endfor %}
  </tr>
{% endfor %}
</table>

I've called the days loop variable 'dday' to make it clear that the lookup item.day here is actually getting item['day'].

Daniel Roseman
+1 for solving the problem
George