List Info

Thread: explicit array access in django templates




explicit array access in django templates
user name
2006-09-28 14:08:15
Let's say I have two arrays:
data=[["a",1,9],["b",2,8],["c"
,3,7],["d",4,6],["e",5,5]]
type=['string','number','number]

I iterate through the 'data' array (either with 'for' or
'range' loops)
and render a <table> with values from the array.
...
range rownumber from 0 to sizeOfData
...
 range columnnumber from 0 to sizeOfColumn
  <td type="{{type.columnnumber}}">
{{data.rownumber.columnnumber}}
</td>
...

For some reason, type.columnnumber and
data.rownumber.columnnumber seem
to print empty strings.

The context does indeed have the two arrays...I can't figure
out why I
get blanks when every thing else seems to work fine.  I'm
obviously
brand new to django templates, am I doing something
obviously wrong?

Thanks.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

explicit array access in django templates
user name
2006-09-28 20:12:22
On Sep 28, 2006, at 7:08 AM, falcon wrote:
>
> Let's say I have two arrays:
>
data=[["a",1,9],["b",2,8],["c"
,3,7],["d",4,6],["e",5,5]]
> type=['string','number','number]
>
> I iterate through the 'data' array (either with 'for'
or 'range'  
> loops)
> and render a <table> with values from the array.
> ..
> range rownumber from 0 to sizeOfData
> ..
>  range columnnumber from 0 to sizeOfColumn
>   <td type="{{type.columnnumber}}">
{{data.rownumber.columnnumber}}
> </td>
> ..
>
> For some reason, type.columnnumber and
data.rownumber.columnnumber  
> seem
> to print empty strings.
>
> The context does indeed have the two arrays...I can't
figure out why I
> get blanks when every thing else seems to work fine. 
I'm obviously
> brand new to django templates, am I doing something
obviously wrong?



You cannot append a variable onto a list variable to use its
value as  
an index into the list. columnumber is not an attribute of
the type  
list, nor is rownumber an attribute of the data list.

In addition, range is not a standard Django template tag,
where did  
you find that? As I mentioned, you cannot index the list
variables,  
therefore a range template tag would not do much good. Most
of the  
time, an iterator is more efficient than indexing into an
array.

What I would do is this:

{% for d in data %}
{% for t in type %}
<td type="{{ t.0 }}">{{ d.0
}}</td><td type="{{ t.1 }}">{{ d.1
}}</ 
td><td type="{{ t.2 }}">{{ d.2
}}</td>
{% endfor %}
{% endfor %}

But since you already know the types of the three columns, I
would  
just go ahead and hard code them into the template.

Don


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

explicit array access in django templates
user name
2006-09-29 02:44:36
Don,
First of all I have to admit that I was testing with jinja
templates,
which are supposed to be based on django (i wanted to run
into just
these kinds of limits before setting up django).

Unfortunately I don't know hwo many columns my table will
have, I want
a generic solution which requires that I iterate over
columns as well
as rows.  Is there really no way to index into an array? 
Are there
work-arounds, perhaps iterating over two variables at the
same time
({%for x,y in data,type%})? I didn't see any thing in the
documentation
that might help (i had some hope for the splice tag but I
don't think
that is appropriate here).

Thanks


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )