#35602 closed New feature (wontfix)
Allow simple_tag to be defined and used in a for loop
Reported by: | Henrique Lacreta Alves | Owned by: | |
---|---|---|---|
Component: | Template system | Version: | 4.2 |
Severity: | Normal | Keywords: | tag, simple_tag, forloop |
Cc: | Henrique Lacreta Alves, Baptiste Mispelon, Tom Carrick, Carlton Gibson | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Currently, simple_tag cannot be used in the context of a loop, e.g.:
{% for object in simple_tag %}
I'm assuming this is not intended, because the following code works with a filter tag (and is the workaround to avoid having to create a new HTML template with inclusion tags):
{% for object in ""|filter_tag %}
This was tested in 4.2 version, not in 5.0 yet.
Change History (5)
comment:1 by , 5 months ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Summary: | Fix simple_tag use in for loop → Allow simple_tag to be defined and used in a for loop |
Type: | Bug → New feature |
comment:2 by , 5 months ago
Fair point, I didn't consider using "as", and it does seem a bit of a stretch extending the use of "simple_tag" when such a simple solution already exists.
One thing I would consider though would be to add this as an example on Documentation. Using "simple_tag" as an iterable looks like a quite common scenario, and replacing it by an inclusion tag is an overkill most of the time. Do you think this would make sense/should I create a documentation suggestion?
comment:3 by , 5 months ago
Cc: | added |
---|---|
Keywords: | forloop added |
On your earlier point that {% for object in ""|filter_tag %}
works, #19882 is related.
I might have been too hasty and am not sure whether this should be a bug as you originally thought. Going to invite some other opinions
Do you think this would make sense/should I create a documentation suggestion?
Possibly, though the docs for the as argument of simple_tag read quite well to me
comment:4 by , 5 months ago
Looking at the code for do_for(), this usage could never have been intended, so agreed it would be a new feature.
Would that be worthwhile? I'd want a proof-of-concept showing what it might look like before saying. (The existing as
approach is perfectly serviceable, if arguably less fluent.)
comment:5 by , 5 months ago
I've always known / thought that you can't use tags in other tags like this, but you can use filters. This seems like the expected behaviour to me. If it's worth doing or not, don't really have an opinion, I would suggest it's not really worth it as simple_tag
already supports as
, which is intended for this kind of thing.
And I think if we did this for simple_tag
, people would undoubtedly wonder why it doesn't work for any tag, as they look identical in the template code. And that would be more effort, and would add to the allowed complexity of the template engine, which is against it's design ideals to keep templates simple (IMO). Better to put this type of logic in the view or use jinja if you need this kind of thing.
Improved docs are always nice, but I would say this isn't a bug.
You're right, given a simple_tag like
You cannot do
{% for object in iterable 1 2 %}{{ object }}{% endfor %}
but you can doI would class wanting to avoid using "as" to be a new feature request and if this is something you feel strongly about, you can discuss this on the Django forum and see if the community agrees with you.
Personally I'm not sure it's worth it