Is it possible to create a template tag that will create a list that a
for loop can loop over? I know I can create a template tag that will
store the list in a variable and then iterate over that variable.  I
want to do it in one step.

For example the template tag with be “list_print” that will create a
list.  I want something like the following in the template:
{% for a in list_print %}

Below is my code.  When I call it the loop never iterates.
@register.tag(name="list_print")
def do_list_print(parser, token):
    #test that don't have arguments
    try:
        tokenList = token.split_contents()

    except Exception:
        raise template.TemplateSyntaxError("%r tag throw unexpected
exception(token.split_contents)" % token.contents.split()[0])

    if len(tokenList)>1:
        raise template.TemplateSyntaxError("%r tag does not take
arguments" % token.contents.split()[0])

    return ListPrintNode()

class ListPrintNode(Node):
    """
    thread safe version based on CycleNode from
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
    """

    def __init__(self):
        self.myList = ['a', 'b']

    def render(self, context):

        if self not in context.render_context:
            context.render_context[self] =
itertools.cycle(self.myList)

        cycle_iter = context.render_context[self]
        return cycle_iter.next()

Brian

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to