I think what people are saying here is that your number, the iteration
limit, has to be coming from somewhere or something. Chances are, that
something is an iterable, or can be made into an iterable very easily,
and thus can be used in a for loop. Where is the number coming from?
On Apr 18, 2008, at 8:23 AM, Kip Parker wrote:
>
> I needed something like this to repeat a part of the template x times,
> but I couldn't get this to work, the filter repeatedly returned the
> range(0,10) so gave a recursion depth error. I may have set it up
> wrong.
>
> I ended up making a repeat tag:
>
> def do_repeat(parser, token):
> try:
> # Splitting by None == splitting by spaces.
> tag_name, arg = token.contents.split(None, 1)
> number = int(arg)
> except ValueError:
> raise template.TemplateSyntaxError, "Repeat tag requires
> exactly one
> argument which must be a number"
> nodelist = parser.parse(('endrepeat',))
> parser.delete_first_token()
> return RepeatNode(nodelist, number)
>
> class RepeatNode(template.Node):
> def __init__(self, nodelist, number):
> self.nodelist = nodelist
> self.number = number
> def render(self, context):
> output = self.nodelist.render(context)
> return output*self.number
> register.tag('repeat', do_repeat)
>
> but it really feels like it shouldn't be that hard. Maybe I missed the
> easy way?
>
>
> On Apr 15, 7:31 pm, Michael <[EMAIL PROTECTED]> wrote:
>> I feel like something like this already exists somewhere, but you
>> can simply
>> write a filter that turns a number into a range:
>>
>> so in your templates you would have:
>>
>> {% for i in 10|range %}
>> ...
>> {%endfor%}
>>
>> and your template filter would simply be:
>>
>> from django.template.defaultfilters import stringfilter
>>
>> @stringfilter
>> def range(value): return range(int(value))
>>
>> On Tue, Apr 15, 2008 at 12:07 PM, Kenneth Gonsalves
>> <[EMAIL PROTECTED]>
>> wrote:
>>
>>
>>
>>> On 15-Apr-08, at 9:04 PM, Darryl Ross wrote:
>>
>>>> Duke wrote:
>>>>> They are looping over a list
>>>>> I am looking for
>>>>> for (i = 0; i < 10; i++) {
>>>>> printf("Hello, World!);
>>>>> }
>>>>> link for looping statement
>>
>>>> I am not aware of any tag that will allow you to do that, out of
>>>> the box. You have two options, the first is to create a custom
>>>> template tag that do what you want[1]. This shouldn't really be
>>>> terribly difficult to do.
>>
>>>> The second option would be to just pass in a variable into the
>>>> context with a list containing the number of items of the number of
>>>> times you want to loop. Using generic views, this could be done in
>>>> your urls.py like:
>>
>>>> ...
>>>> ('^$', 'direct_to_template',
>>>> { 'template_name': 'homepage.html',
>>>> 'extra_context': {'looper': range(10) }})
>>>> ...
>>
>>>> Then you can use the standard {% for %} tag:
>>
>>>> {% for i in looper %}
>>>> {{i}}
>>>> {% endfor %}
>>
>>>> [1]http://www.djangoproject.com/documentation/templates_python/
>>>> #extending-the-template-system
>>
>>> where is the use case for this? I cannot conceive of any situation
>>> where one would want to loop over an arbitrary number.
>>
>>> --
>>
>>> regards
>>> kg
>>> http://lawgon.livejournal.com
>>> http://nrcfosshelpline.in/code/
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---