djangosnippets.org: Latest snippets tagged with 'template-tags'https://djangosnippets.org/tags/template-tags/2010-09-15T19:30:00.441390-05:00Template tag to create a list from one or more variables and/or literals
2010-09-15T19:30:00.441390-05:00davidchambershttps://djangosnippets.org/snippets/2196/<p>This code is taken from a <a href="http://stackoverflow.com/questions/3715550/creating-a-list-on-the-fly-in-a-django-template/3715794#3715794">Stack Overflow answer by Will Hardy</a>.</p>
<p>Usage: <code>{% collect var1 var2 'foo' 'bar' 5 as some_list %}</code>.</p>
<p>Sometimes one wishes to create a list on the fly within a template. Perhaps a collection needs to be passed to a template filter, but …</p>
Freely redistributableregister.tag as a class decorator
2010-01-27T19:24:22.605147-06:00gsakkishttps://djangosnippets.org/snippets/1894/<p>Defining a custom template tag consists of three parts: a compiling function, a rendering <code>Node</code> subclass and a tag registration with <code>register.tag()</code>. The latter can be used as a (function) decorator on the compiling function, simplifying things into two parts.</p>
<p>A neat fact is that <code>register.tag()</code> can actually be …</p>
Freely redistributableJinja2 integration + application specific functions/filters/tests
2008-10-05T01:15:39.249535-05:00hasenjhttps://djangosnippets.org/snippets/1112/<p>Jinja2 is an alternative template system that can be plugged into django.
It offers greator flexibility in presentation logic; if you find the django template system too restrictive, you should have a look at Jinja2
(The syntax is very similar).</p>
<p>In Jinja, you don't need costum tags (most of the …</p>
Freely redistributableMod to allow simple_tag to access context
2008-09-27T12:26:06.937711-05:00leafhttps://djangosnippets.org/snippets/1089/<p>This is a mod I made to the Django simple_tag system to let the simple_tags access comments. I plan to try and get it integrated into the trunk, so it's mainly here so (a) the people on django-developers can see it, and (b) while I'm waiting, or if it doesn't …</p>
Freely redistributableTemplate tag: split list to n sublists
2008-07-16T14:15:06.251690-05:00movieladyhttps://djangosnippets.org/snippets/889/<p>Based on <a href="http://www.djangosnippets.org/snippets/660/">this snippet</a>, adapted to split a list into <em>n</em> number of sublists, e.g. split a list of results into three evenly-divided sublists to enable display of the results in three columns on one page with CSS.</p>
<p>Tag: <code>{% list_to_columns your_list as new_list number_of_columns %}</code></p>
<p>The split_seq solution …</p>
Freely redistributableself-related objects list with links
2007-11-20T10:15:45.943587-06:00kumbryhttps://djangosnippets.org/snippets/475/<p>The problem was to output self-related objects (like category objects which may be a sub-category in any category and so on) in unordered list with links to certain view (providing some object.id arg). Its somewhat like unordered_list tag but takes plain list of objects and provides links.</p>
<p>For example:</p>
<p>category_list …</p>
Freely redistributable