On Sat, 2006-09-23 at 00:12 +1000, Woolley wrote:
> Hi,
>
> I was wondering how to read a sparse martrix in a django template. I can
> generate the matrix in the view function, but I cannot seem to get the
> results to come out.
>
> Basic assignment/ format below, not how it is really done though, but
> this is the effective result
>
> matrix[(1,0)] = 1
> matrix[(1,1)] = 2
> matrix[(1,2)] = 3
> matrix[(2,0)] = 4
> matrix[(2,1)] = 5
> matrix[(2,2)] = 6
>
> How would I get the template language to understand this. It seems to
> tie itself in a knot when I use {{ matrix.(1,0) }}
>
> The follow on question to this, is can the first number be a variable,
> thereby cutting the template size down?
>
> eg {{ matrix.({{ order }},0) }}, this particular format does fail though.
Two possibilities I can think of:
(1) Change your data structure slightly so that it is nested sequences:
matrix[1][0] = 1
matrix[1][1] = 2, etc
Then you can do
{% for row in matrix %}
{% for element in row %}
{{ element }}
{% endfor %} <br>
{% endfor %}
(2) Using your original data structure, we can fake iterating overs the
first index and then the second. This is pretty evil and untested, but
it should be close to correct. Note that this comes pretty close to
programming in templates (in fact it completely crosses the line), so I
would suggest the slight change in data structure, above, if at all
possible.
{% for item in matrix.items|dictsort:"0" %}
{% ifchanged item.0.0 %}<br>{% endif %}
{{ item.1 }} <!-- This is the element content -->
{% endfor %}
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---