Thanks for the response Aidas! It was a big help to know that it was
not
possible. It got around the problem by writing a custom tag "ifin". So
I can do:
{{ ifin item list_of_items }}
html to output if item is contained in the list
{{ endifin }}
This tag neatly solves my original problem.
In case this tag is useful to anyone else, here is the code in
mytags.py:
----------- BEGIN ---------
# modified code from django/template/defaulttags.py
from django.template import Node, NodeList, Template, Context,
resolve_variable
from django.template import TemplateSyntaxError, VariableDoesNotExist,
BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END,
SINGLE_BRACE_START, SINGLE_BRACE_END
from django.template import get_library, Library,
InvalidTemplateLibrary
from django.conf import settings
import sys
register = Library()
class IfInNode(Node):
def __init__(self, var1, var2, nodelist_true, nodelist_false,
negate):
self.var1, self.var2 = var1, var2
self.nodelist_true, self.nodelist_false = nodelist_true,
nodelist_false
self.negate = negate
def __repr__(self):
return "<IfInNode>"
def render(self, context):
try:
val1 = resolve_variable(self.var1, context)
except VariableDoesNotExist:
val1 = None
try:
val2 = resolve_variable(self.var2, context)
except VariableDoesNotExist:
val2 = None
if (self.negate and not (val1 in val2)) or (not self.negate
and val1 in val2):
return self.nodelist_true.render(context)
return self.nodelist_false.render(context)
def do_ifin(parser, token, negate):
"""
Output the contents of the block if the 1st arg is in the 2nd arg
(or NOT)
Examples::
{% ifin item item_list %}
...
{% endifin %}
{% ifnotin item item_list %}
...
{% else %}
...
{% endifnotin %}
"""
bits = list(token.split_contents())
if len(bits) != 3:
raise TemplateSyntaxError, "%r takes two arguments" % bits[0]
end_tag = 'end' + bits[0]
nodelist_true = parser.parse(('else', end_tag))
token = parser.next_token()
if token.contents == 'else':
nodelist_false = parser.parse((end_tag,))
parser.delete_first_token()
else:
nodelist_false = NodeList()
return IfInNode(bits[1], bits[2], nodelist_true, nodelist_false,
negate)
def ifin(parser, token):
return do_ifin(parser, token, False)
ifin = register.tag(ifin)
def ifnotin(parser, token):
return do_ifin(parser, token, True)
ifnotin = register.tag(ifnotin)
-------- END ---------
On Feb 2, 6:19 am, "Aidas Bendoraitis" <[EMAIL PROTECTED]>
wrote:
> I think, it's impossible to use forloop.counter0 as an index, because
> array.forloop already searches for array['forloop'], array[forloop],
> array.forloop, and similar combinations which don't exist. But maybe
> you could zip(items, array) in the view or merge items and array in
> some other way and to get all the necessary values from that merged
> list.
>
> Good luck!
> Aidas Bendoraitis aka Archatas
>
> On 2/1/07, Brandon Warren <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hello,
>
> > Is it possible to use forloop.counter0 as an index into a list?
>
> > For example:
>
> > in my view I have:
>
> > array=[10,11,12,13]
>
> > in my template I have:
>
> > {% for item in items %}
> > counter = {{ forloop.counter0 }}<BR>
> > 1st val of array = {{ array.0 }}<BR>
> > nothing appears here: {{array.forloop.counter0 }}<BR>
> > ---------------<BR>
> > {% endfor %}
>
> > This is what comes out the browser:
> > counter = 0
> > 1st val of array = 10
> > nothing appears here:
> > ---------------
> > counter = 1
> > 1st val of array = 10
> > nothing appears here:
> > ---------------
>
> > Thanks in advance,
> > Brandon
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---