<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codegrind &#187; recursive</title>
	<atom:link href="http://jordanovski.com/tag/recursive/feed" rel="self" type="application/rss+xml" />
	<link>http://jordanovski.com</link>
	<description>Homepage of Dusko Jordanovski</description>
	<lastBuildDate>Fri, 18 Jun 2010 17:37:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A recursive django template tag</title>
		<link>http://jordanovski.com/a-recursive-django-template-tag</link>
		<comments>http://jordanovski.com/a-recursive-django-template-tag#comments</comments>
		<pubDate>Fri, 05 Jun 2009 19:37:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[template tag]]></category>

		<guid isPermaLink="false">http://jordanovski.com/?p=169</guid>
		<description><![CDATA[Here is my attempt to create a "silver bullet" tag for printing tree structures with the Django templating language. It's far from a silver bullet, tho, but it can do basic stuff:
It's a modification of the standard "for" tag and i have kept the counter, counter0, first and last variables, only this time they are [...]]]></description>
			<content:encoded><![CDATA[<p>Here is my attempt to create a "silver bullet" tag for printing tree structures with the Django templating language. It's far from a silver bullet, tho, but it can do basic stuff:</p>
<p>It's a modification of the standard "for" tag and i have kept the counter, counter0, first and last variables, only this time they are not attributes to forloop, but rather to recurseloop. Check the docstring for more info.</p>
<p>For example, if you need to print comments that have other comments as replies, you would want the comments to appear one below the other, but the replies to be indented a bit. Let's say 20 pixels per level. So the code would be:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> load file_that_contains_recurse_tag <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> recurse comment <span style="color: #ff7700;font-weight:bold;">in</span> comments children=<span style="color: #483d8b;">&quot;replies&quot;</span> indent=<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
  <span style="color: #66cc66;">&lt;</span>div style=<span style="color: #483d8b;">'margin-left:{{indent}}px;'</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> comment.<span style="color: black;">text</span> <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
  <span style="color: #66cc66;">&lt;</span>/div<span style="color: #66cc66;">&gt;</span>
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endrecurse <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>

<p>This tag will expect a list of comments (top-level) that have a property named 'replies' which contain other comments.</p>
<p>indent is an argument that will start with the float value of 0 and get increased by 20 on each depth level of the recursion. You can pass as many variables like indent as you like. They must be in the form</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">name=<span style="color: black;">&#40;</span><span style="color: #008000;">float</span>,<span style="color: #008000;">float</span><span style="color: black;">&#41;</span></pre></div></div>

<p>or strings will also work but must be enclosed in quotes</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">name=<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;string&quot;</span>,<span style="color: #483d8b;">&quot;string&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Caveat: You must not leave blank spaces between the equal signs when assigning children and additional incremented arguments. I will fix this later.</p>
<p>A second scenario is when you need the parent element to <strong>contain </strong>the children, like in unordered/ordered lists. In that case you can use the {% yield %} tag inside the recurse block. This tag will output the HTML between the recurse and endrecurse tags if there are any children in the current iteration item, or it will output nothing if there are no children.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> recurse comment <span style="color: #ff7700;font-weight:bold;">in</span> comments children=<span style="color: #483d8b;">&quot;replies&quot;</span> indent=<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
<span style="color: #66cc66;">&lt;</span>div style=<span style="color: #483d8b;">'margin-left:20px;'</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> comment.<span style="color: black;">text</span> <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
  <span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
  <span style="color: #66cc66;">&lt;</span>/div<span style="color: #66cc66;">&gt;</span>
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endrecurse <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>

<p>The yield tag (as for now) can <strong>only </strong>be used directly inside the recurse block, much like the {% else %} tag can only be used directly inside the if-endif block. This means that you can't make code like</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> recurse comment <span style="color: #ff7700;font-weight:bold;">in</span> comments children=<span style="color: #483d8b;">&quot;replies&quot;</span> indent=<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
<span style="color: #66cc66;">&lt;</span>div style=<span style="color: #483d8b;">'margin-left:{{indent}}px;'</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> comment.<span style="color: black;">text</span> <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&lt;</span>/div<span style="color: #66cc66;">&gt;</span>
  <span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">if</span> cond <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
    <span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
  <span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endif <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> endrecurse <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>

<p>This will fail with an invalid block tag exception. You can check the docstring of the do_recurse function inside the code for more info. Also, you may want to check this code for errors since I just wrote it today, and haven't had much time to test it.</p>
<p><a href="http://jordanovski.com/wp-content/uploads/recurse.zip">Download and comment on any errors you find</a> :)</p>
]]></content:encoded>
			<wfw:commentRss>http://jordanovski.com/a-recursive-django-template-tag/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
