Template language lacks means to display trees as nested structures
|Reported by:||NickMoffitt||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I have a fairly simple need: to display tree-structured data as nested tags. For my example, I'll propose that what is displayed is a list of links to messages in a complicated thread structure. This is clearly a display logic problem, and is thus the province of the template system. This is also common enough of a problem that I do not feel it should be necessary to cobble together custom template tags to meet my needs.
The core problem is that the templating language does not support recursion or even the mutable structures necessary to simulate it. If I had some sort of stack that I could work with, this would be a solvable problem. I understand that this is probably good for performance reasons, and it prevents endless loops and runaways that are probably more dangerous in a template than in other places.
In the #django channel on freenode, I received many helpful proposals: Recursive loading of templates was one, although this was pointed out as being something of a performance nightmare.
Another was to serialize my data into a traversal, with tags showing indent and outdent. The trouble with this is that there's no easy way with the built-in tags and filters to do something like say "print as many </ol> tags as the value in message.outdent". To avoid completely embedding HTML in my structures, I had to make the outdent properties into lists, so that I could iterate over them in a for loop and print a closing tag each time. This is plainly bogus, but it gets the job done without needing any custom tags.
I often see people ask "why are you so reluctant to write a custom tag?" in response to questions about the template system. Perhaps the frequency of this question is a hint that the base tag and filter set need to be fleshed out a little better. in the channel someone noticed that jinja has tackled this problem using loop recursion tags.
Change History (4)
comment:1 Changed 8 years ago by NickMoffitt <nick@…>
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:3 Changed 8 years ago by mtredinnick
- Resolution set to wontfix
- Status changed from new to closed