Generate a tree List...

50 views
Skip to first unread message

Jens Diemer

unread,
Feb 27, 2007, 4:52:42 AM2/27/07
to django...@googlegroups.com
I wand generate a tree menu/sitemap.
How can i make a recursive loop in a template???

In jinja i can use the tag "recurse":
http://wsgiarea.pocoo.org/jinja/docs/loops.html#recursion

Here a jinja example:
------------------------------------------------------------------------
context = {'sitemap': [{'href': u'/Index/',
'subitems': [{'href': u'/Index/PhpBBadmin/',
'title': u'phpBBadmin'}],
'title': u'index'},
{'href': u'/ExamplePages/',
'subitems': [{'href': u'/ExamplePages/TextileExample/',
'title': u'complete tinyTextile examples'},
{'href': u'/ExamplePages/Testpage/',
'title': u'a testpage ;)'},
{'href': u'/ExamplePages/SourceCode/',
'title': u'SourceCode'},
{'href': u'/ExamplePages/Contact/',
'title': u'contact'},
{'href': u'/ExamplePages/SiteMap/',
'title': u'SiteMap'}],
'title': u'example pages'},
{'href': u'/Test/', 'title': u'test'}]}


template = """
<h1>Sitemap</h1>
<ul>
{% for item in sitemap %}
<li>
<a href="{{ item.href }}">{{ item.title|escapexml }}</a>
{% if item.subitems %}
<ul>{% recurse item.subitems %}</ul>
{% endif %}
</li>
{% endfor %}
</ul>
"""
------------------------------------------------------------------------

recurse used the for loop again with the subitems. So i can easy create
a recursion.

How can i do this in django?


--
Mfg.

Jens Diemer


----
CMS in pure Python CGI: http://www.pylucid.org

Joseph Heck

unread,
Feb 28, 2007, 4:12:50 PM2/28/07
to django...@googlegroups.com
The templates don't support that kind of functionality directly. You'll probably want to create your own template tag that will do for you - you can recurse to your heart's content in Python.

Check out the docs at http://www.djangoproject.com/documentation/templates_python/ for a good overview. Making one is really pretty straightfoward.

Jeremy Dunck

unread,
Feb 28, 2007, 5:29:25 PM2/28/07
to django...@googlegroups.com
On 2/27/07, Jens Diemer <python...@jensdiemer.de> wrote:
...

> recurse used the for loop again with the subitems. So i can easy create
> a recursion.

While I agree with Joseph that writing your own template tag to handle
this is pretty easy, it's worth noting that Jinja spun off of Django's
templating specifically to add things like recursion.

It won't be added to Django core, AFAIK.

Jens Diemer

unread,
Mar 1, 2007, 3:48:09 AM3/1/07
to django...@googlegroups.com
Joseph Heck schrieb:

> Check out the docs at
> http://www.djangoproject.com/documentation/templates_python/ for a good
> overview. Making one is really pretty straightfoward.

I don't think this is so easy :(

I found this:
https://svn.greenpeace.org/projects/custard/browser/production/trunk/melt/apps/custard/templatetags/customtags.py

But i don't know how i can use this.

Jeremy Dunck schrieb:


> It won't be added to Django core, AFAIK.

Why?
Everyone need this function, if you would like to build a sitemap with a
tree.

I think this feature should not be missing in the core. It's a very
useful function

Jeremy Dunck

unread,
Mar 1, 2007, 9:35:12 AM3/1/07
to django...@googlegroups.com
On 3/1/07, Jens Diemer <python...@jensdiemer.de> wrote:
>
> Joseph Heck schrieb:
> > Check out the docs at
> > http://www.djangoproject.com/documentation/templates_python/ for a good
> > overview. Making one is really pretty straightfoward.
>
> I don't think this is so easy :(
>
> I found this:
> https://svn.greenpeace.org/projects/custard/browser/production/trunk/melt/apps/custard/templatetags/customtags.py
>
> But i don't know how i can use this.

Yeah, I'd be careful about the license. I don't see one.
You might contact one of these folks:
https://svn.greenpeace.org/projects/custard/browser/doc/copyright.html

But it looks like something like this would do:

x = { 'value':1, 'children': [
{ 'value': 2, 'children': []},
{'value' : 3, 'children': [
{ 'value':4, 'children':[] }
]}
]}
----
{% recurse through children as node starting with x %}
...do something with node.
{% endrecurse %}


> Jeremy Dunck schrieb:
> > It won't be added to Django core, AFAIK.
> Why?
> Everyone need this function, if you would like to build a sitemap with a
> tree.

No, not everyone needs this.
http://www.djangobook.com/en/beta/chapter04/#cn210

If you do, consider using Jinja with Django. The HttpResponse just
wants a string which Jinja is (of course) capable of creating.

> I think this feature should not be missing in the core. It's a very
> useful function

As a programmer, I agree. But the Django templating language is not
only for me, nor am I limited to the Django templating language.

Jens Diemer

unread,
Mar 2, 2007, 2:29:24 AM3/2/07
to django...@googlegroups.com
Jeremy Dunck schrieb:

>> I found this:
>> https://svn.greenpeace.org/projects/custard/browser/production/trunk/melt/apps/custard/templatetags/customtags.py
>>
>> But i don't know how i can use this.
>
> Yeah, I'd be careful about the license. I don't see one.
> You might contact one of these folks:
> https://svn.greenpeace.org/projects/custard/browser/doc/copyright.html

No problem, the source is under the GPL and PyLucid is under GPL, too ;)


> But it looks like something like this would do:
>
> x = { 'value':1, 'children': [
> { 'value': 2, 'children': []},
> {'value' : 3, 'children': [
> { 'value':4, 'children':[] }
> ]}
> ]}
> ----
> {% recurse through children as node starting with x %}
> ...do something with node.
> {% endrecurse %}

Thank you for your help! Now, i try it to use it ;)

Jens Diemer

unread,
Mar 2, 2007, 3:03:43 AM3/2/07
to django...@googlegroups.com
Jens Diemer schrieb:

> Jeremy Dunck schrieb:
>>> I found this:
>>> https://svn.greenpeace.org/projects/custard/browser/production/trunk/melt/apps/custard/templatetags/customtags.py
>>>
>>> But i don't know how i can use this.
>> Yeah, I'd be careful about the license. I don't see one.
>> You might contact one of these folks:
>> https://svn.greenpeace.org/projects/custard/browser/doc/copyright.html
>
> No problem, the source is under the GPL and PyLucid is under GPL, too ;)

Sorry i forgot the link to the license:

https://svn.greenpeace.org/projects/custard/browser/production/trunk/melt/COPYING

Jens Diemer

unread,
Mar 2, 2007, 4:21:01 AM3/2/07
to django...@googlegroups.com
Jeremy Dunck schrieb:

Hm :(
The code is for an older django version.

Jeremy Dunck

unread,
Mar 2, 2007, 9:00:35 AM3/2/07
to django...@googlegroups.com
On 3/2/07, Jens Diemer <python...@jensdiemer.de> wrote:
>
> Jeremy Dunck schrieb:
> >> I found this:
> >> https://svn.greenpeace.org/projects/custard/browser/production/trunk/melt/apps/custard/templatetags/customtags.py
> >>
> >> But i don't know how i can use this.
>
> Hm :(
> The code is for an older django version.

If you want recursion in templates, why not just use Jinja?

Jens Diemer

unread,
Mar 2, 2007, 9:17:51 AM3/2/07
to django...@googlegroups.com
Jeremy Dunck schrieb:

> If you want recursion in templates, why not just use Jinja?

Jinja is super cool. But I would like to keep the PyLucid package small.

I thought with django i would choose a full features framework :(

Jeremy Dunck

unread,
Mar 2, 2007, 9:24:07 AM3/2/07
to django...@googlegroups.com
On 3/2/07, Jens Diemer <python...@jensdiemer.de> wrote:
>
> I thought with django i would choose a full features framework :(

It works pretty well for me and many others. Writing a template tag
is a reasonable solution, IMHO. :-/

Reply all
Reply to author
Forward
0 new messages