recursive template calls

132 views
Skip to first unread message

Shannon -jj Behrens

unread,
Feb 7, 2006, 10:31:16 PM2/7/06
to django...@groups.google.com
Hi,

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

Cheng Zhang

unread,
Feb 7, 2006, 10:59:34 PM2/7/06
to django...@googlegroups.com
Please search the previous email archive for discussion on such topic
like "tree view".
In fact, there is a wiki page for an implementation as well.

Jamie Scheinblum

unread,
Feb 7, 2006, 11:45:07 PM2/7/06
to django...@googlegroups.com
that's funny. they answered how to do the tree thing, but the wiki
basically says no recursion... I should look into the % recurse %
keyword tho.

-j

Max Battcher

unread,
Feb 8, 2006, 4:39:01 AM2/8/06
to django...@googlegroups.com
Jamie Scheinblum wrote:
> that's funny. they answered how to do the tree thing, but the wiki
> basically says no recursion... I should look into the % recurse %
> keyword tho.

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/

Shannon -jj Behrens

unread,
Feb 8, 2006, 3:01:31 PM2/8/06
to django...@googlegroups.com
Sorry, guys, I'm confused. I *did* search before posting, but wasn't
able to find a full answer. I saw someone doing the bulletin board
example, but I never found out how to recursively call a template.
Must I use the non Django {% recurse %} custom tag?

Thanks,
-jj

Julio Nobrega

unread,
Feb 9, 2006, 6:50:53 AM2/9/06
to django...@googlegroups.com
You don't *need* recursion on templates for threaded messages like
your example app, that's exactly the point :)


--
Julio Nobrega - http://www.inerciasensorial.com.br

Shannon -jj Behrens

unread,
Feb 9, 2006, 4:26:04 PM2/9/06
to django...@googlegroups.com
> You don't *need* recursion on templates for threaded messages like
> your example app, that's exactly the point :)

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

Robert Wittams

unread,
Feb 11, 2006, 7:08:12 AM2/11/06
to django...@googlegroups.com

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

Luke Plant

unread,
Feb 11, 2006, 8:31:06 AM2/11/06
to django...@googlegroups.com
On Saturday 11 February 2006 12:08, Robert Wittams wrote:

> 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/

Shannon -jj Behrens

unread,
Feb 13, 2006, 5:44:45 PM2/13/06
to django...@googlegroups.com

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

Jonathan Ellis

unread,
Feb 13, 2006, 7:09:27 PM2/13/06
to django...@googlegroups.com
On 2/13/06, Shannon -jj Behrens <jji...@gmail.com> wrote:

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.

 
OTOH Python is a lot cleaner than attempts at "designer-friendly" mini-languages.  (CFScript, ActionScript, ...)

-Jonathan

luke....@gmail.com

unread,
Feb 16, 2006, 4:58:35 AM2/16/06
to Django users
Shannon -jj Behrens wrote:

> 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

Reply all
Reply to author
Forward
0 new messages