#5172 closed (wontfix)
Extend the {% for %} tag to work as a simple loop
Reported by: | Chris Beaven | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | |
Severity: | Keywords: | for-tag enhancement feature | |
Cc: | me@…, danielcristian@… | Triage Stage: | Design decision needed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
It's come up several times in IRC that people wanted to be able to just do a simple loop (repeat this X times) in their template.
The attached patch extends the {% for %}
tag so it can also work like this:
{% for 5 %} repeat me {% endfor %}
It also works with integer variables and the forloop
variable is still available within the loop:
{% for my_integer %} loop: {{ forloop.counter }} {% endfor %}
Attachments (2)
Change History (16)
Changed 16 years ago by
Attachment: | simple-for.patch added |
---|
comment:1 Changed 16 years ago by
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:2 Changed 16 years ago by
Hmm, this one merits some discussion. I like the general idea but need to mull over the syntax.
comment:3 Changed 16 years ago by
Keywords: | feature added |
---|
Changed 16 years ago by
Attachment: | numerical-foo-loop.diff added |
---|
comment:4 Changed 16 years ago by
Needs documentation: | set |
---|
I added a patch of my own. It's more complex than SmileyChris' and needs documentation (and possibly more tests.) Let me know what you think of the general syntax. The patch is attached.
Example usage:
{% for i 1 to 5 %} {{ i }} {% endfor %} {% for i 1 to 5 by 2 exclusive %} {{ i }} {% endfor %} {% for i 5 to 1 by -2 %} {{ i }} {% endfor %}
comment:5 Changed 16 years ago by
Ugh, I don't like at all, sorry gnuvince. The aim isn't to make Django templates into python...
comment:6 follow-up: 7 Changed 16 years ago by
Cc: | me@… added |
---|
Why a significant syntax change instead of a one-liner filter?
@register.filter def times(count): return range(int(count))
{% for item in 5|times %} repeat me {% endfor %}
comment:7 Changed 16 years ago by
Replying to durdinator:
Why a significant syntax change instead of a one-liner filter?
Because it looks simpler (and it's a syntax extension, really) and because then you're stuck with a pointless item
context var.
Neither of which are convincing arguments. And talking about this at sprint, it seemed like the core would just prefer to have a new filter named something separate anyway so I don't hold much hope for my patch really :)
comment:8 Changed 16 years ago by
How about something like:
{% times 5 %} {{ times.iter0 }} {{ times.iter1 }} <br /> {% endtimes %} # Output 0 1 1 2 2 3 3 4 4 5
comment:9 Changed 16 years ago by
P.S.: times is just a tentative name. I think repeat would be better.
comment:10 Changed 16 years ago by
And that's why I like it attached to {% for %}
- then you get all of the forloop
context variable goodness for free.
comment:11 follow-up: 14 Changed 16 years ago by
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I just can't come up with a use case that isn't something better done in a view or better done by just writing out the rows by hand. Besides, this is a pretty simple thing to add as a custom tag in your app.
comment:12 Changed 15 years ago by
Cc: | danielcristian@… added |
---|---|
Resolution: | wontfix |
Status: | closed → reopened |
I had this model:
class Mana(models.Model): name = models.CharField(u'Mana', max_length=200, unique=True) image = models.ImageField(u'Symbol', upload_to='manas/') class Cost(models.Model): card = models.ForeignKey(Card) quantity = models.IntegerField('Quantity', max_length=2) mana = models.ForeignKey(Mana, related_name=u'mana', verbose_name=u'Mana')
(Yes, it's related to Magic Cards...)
Some cards had a cost with a quantity value (1 Forest, 3 Islands, 5 Mountains), and I need to loop quantity times to show n-times the image cost.
It could be done with this kind of loop.
comment:13 Changed 15 years ago by
Resolution: | → wontfix |
---|---|
Status: | reopened → closed |
Please do not reopen a ticket that's been marked "wontfix" by a core developer; if you strongly disagree, consider starting a discussion on the developers' mailing list.
comment:14 Changed 13 years ago by
Has patch: | unset |
---|---|
Needs documentation: | unset |
Version: | SVN |
Replying to jacob:
I just can't come up with a use case that isn't something better done in a view or better done by just writing out the rows by hand. Besides, this is a pretty simple thing to add as a custom tag in your app.
This ticked is old, but to present you with a scenario that I'm into right now: I need to create a varying number of radiobuttons, depending of the number of choices allowed by the parents settings. The parent has a "number of objects allowed" field in the model. I therefore need to repeat a tag that can do the same thing {% for %}, but not iterating over an array or set, but just a variable number.
Tests and docs included, just need a decision.