I'm trying to build ye ol' bulletin board application to try out
Django. I'm having a hard time outputting the hierarchy of messages.
I can't figure out how to use recursion in templates. I also need to
be able to treat a template "like a function" passing arguments. How
should I be thinking about this problem? Here's the app written in
another framework: <http://seamstress.norcalttora.com/>.
Thanks!
-jj
-j
The {% recurse %} is a custom tag from the Custard work at Greenpeace,
not a Django-contributed tag. It is Open Source under the LGPL.
--
--Max Battcher--
http://www.worldmaker.net/
Thanks,
-jj
--
Julio Nobrega - http://www.inerciasensorial.com.br
Julio, with all due respect for your programming prowess, I *like*
recursion. It can often make hard problems easy, even when generating
HTML.
Anyway, I figured out how to solve the problem *using recursion*, and
I blogged about it:
<http://jjinux.blogspot.com/2006/02/python-recursion-in-django-templates.html>.
It works, but it's nowhere near as elegant as simply being able to
add a function to the template that I could call recursively. :-/
Best Regards,
-jj
This is ridiculous. Something like this seems a lot easier to me...
templatetags.py:
register = template.Library()
@register.inclusion_tag('comment')
def show_comment(comment):
{'comment': comment}
comment.html:
{% load comments %}
{% comment.subject %}
{% comment.body %}
{% for child in comments.children %}
{%show_comment child %}
{% endfor %}
page.html:
{% show_comment comment %}
The moral of the story : custom template tags aren't really an optional
bit of the framework. If you don't learn how to use at least simple_tag
and inclusion_tag, you will get annoyed quite often.
Robert
> If you don't learn how to use at least
> simple_tag and inclusion_tag, you will get annoyed quite often.
That's fairly easy for you to say -- you wrote them! Documentation on
these tags is fairly hard to come by - nothing in the official docs,
nothing in the source code, nothing (that I could find) in the wiki. I
eventually found this:
http://code.djangoproject.com/changeset/1410
Any chance we could put that somewhere more prominent?
Luke
--
Life is complex. It has both real and imaginary components.
Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/
I think it's unfortunately that template writers have to resort to
Python just to package up a bit of HTML into a function, but that's
just my opinion.
-jj
I think it's unfortunately that template writers have to resort to
Python just to package up a bit of HTML into a function, but that's
just my opinion.
> I think it's unfortunately that template writers have to resort to
> Python just to package up a bit of HTML into a function, but that's
> just my opinion.
In the normal case, you can just use the {% include %} tag, so you
don't have to resort to Python. It is the recursion that is bringing
in that necessity.
Luke