On 4/24/06, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
>
> Hi Sam,
>
> On Mon, 2006-04-24 at 15:52 -0400, Sam Tran wrote:
> > Hi All,
> >
> > I have the following list of tuples:
> > [(0, 'Jenna'),
> >  (1, 'Tom'),
> >  (1, 'Paul'),
> >  (2, 'Cathy')]
> >
> > I want to create a table with as many row as the number of tuples (one
> > row for each tuple). The first element of each tuple is the number of
> > tabulations or empty cells to put in each row. The second element is
> > the string to write in the last cell of the row. So the resulting
> > table would look like this:
> >
> > | Jenna                             |
> > |           | Tom                   |
> > |           | Paul                   |
> > |           |             | Cathy   |
>
> I cannot think of any way to do this directly in templates. You need to
> preprocess the data slightly. Initially, I came up with an example that
> converted your prefix numbers into lists of the same length and then
> iterated over those lists to insert </td><td> pairs. Something like
>
>         converted_data = [([None] * d[0], d[1]) for d in data]
>
> in the view and
>
>         {% for entry in data %}
>             <tr><td>
>             {% for padding in entry.0 %}
>                 </td><td>
>             {% endfor %}
>             {{ entry.1 }}</td></tr>
>         {% endfor %}
>
> Then I realised this was stupid and if I was going to preprocess the
> data, I might as well just give it the right string initially. So
>
>         converted_data = [('</td><td>' * d[0], d[1]) for d in data]
>
> in the view and
>
>         {% for entry in data %}
>             <tr><td>{{ entry.0 }}{{entry.1 }}</td></tr>
>         {% endfor %}
>
> in the template.
>
> If it were possible to create a loop from a number (equivalent of
> Python's range() function), rather than iterating over a sequence, the
> first solution would work without pre-processing. But I can't say that I
> dislike the fact you cannot do this. It forces complexity out the
> templates: the temptation to use the templates as a computation engine
> would become too great otherwise.
>

Malcom,

I initially tried to create a loop with range() as you described. I
wanted to avoid doing some 'template' pre-processing in the view file.
But apparently there is no way to do otherwise. I will definitely try
your solution tomorrow.

Your help is greatly appreciated.

Best regards,
Sam

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

Reply via email to