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
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.
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
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.
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 ;)
Sorry i forgot the link to the license:
https://svn.greenpeace.org/projects/custard/browser/production/trunk/melt/COPYING
Hm :(
The code is for an older django version.
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 :(
It works pretty well for me and many others. Writing a template tag
is a reasonable solution, IMHO. :-/