djangosnippets.org: Latest snippets tagged with 'jinja2'https://djangosnippets.org/tags/jinja2/2010-06-12T12:25:05.268956-05:00Django 1.2+ template loader for Jinja2
2010-06-12T12:25:05.268956-05:00SimonSapinhttps://djangosnippets.org/snippets/2063/<p>This is a minimal template loader for Django 1.2 or higher that loads <a href="http://jinja.pocoo.org/2/">Jinja2</a> templates. It is better integrated with Django than using Jinja2 directly:</p>
<ul>
<li>Your view code is the same</li>
<li>Unmodified generic views use it</li>
<li>RequestContext and context processors still work</li>
</ul>
<p>To use it, add the following to you …</p>
Freely redistributablejinja2 csrf_token extension
2009-12-30T13:30:30.419715-06:00jasongreenhttps://djangosnippets.org/snippets/1847/<p>init env</p>
<p><code>env = Envoriment(extensions=('youproject.app.extensions.csrf_token'), loader=loader)</code></p>
<p>or see [http://www.djangosnippets.org/snippets/1844/] and in settings.py:</p>
<p><code>JINJA_EXTS=('jinja2.ext.i18n','youproject.app.extensions.csrf_token',)</code></p>
<p>use this extension in jinja2 template just like django template:</p>
<p><code><form ...>{% csrf_token %}...</form></code></p>
Freely redistributableintegrated jinja2 which could use generic view ,my djangojinja2.py
2009-12-29T13:08:30.036099-06:00jasongreenhttps://djangosnippets.org/snippets/1844/<p>I tried a few snippets of integrated jinja2 in django, which provided ?.render_to_string and ?.render_to_response in the way of jinja2. <strong>But those snippets could not use the generic view</strong>, because of the generic views is use default django template. so i write this snippet which could use generic view, …</p>
Freely redistributableA few jinja2 filters like django ones
2009-03-13T17:22:37.569348-05:00brondsemhttps://djangosnippets.org/snippets/1371/<p>Some of this is modified from <a href="http://www.djangosnippets.org/snippets/1062/">http://www.djangosnippets.org/snippets/1062/</a></p>
Freely redistributablemonkey-patch django to use jinja2 templates for 404/500 pages and 3rd-party apps
2009-03-13T17:17:46.725362-05:00brondsemhttps://djangosnippets.org/snippets/1370/<p>This is heavily inspired by <a href="http://code.google.com/p/smorgasbord/">http://code.google.com/p/smorgasbord/</a>. But that couldn't reuse an existing jinja2 Environment, nor set filters on the Environment it created.</p>
<p>This code assumes that you have <code>env</code> declared previously in the file as your Jinja2 Environment instance.</p>
<p>In <code>settings.py</code>, you should set</p>
<pre><code>KEEP_DJANGO_TEMPLATES = (
'/django/contrib/', …</code></pre>
Freely redistributableUsing Django Generics with Jinja2
2008-12-10T04:10:18.744030-06:00rmthttps://djangosnippets.org/snippets/1241/<p>Jinja2, while a great replacement for Django templates, is not a drop-in replacement for it. I wanted to use Photologue with my Jinja templates, but because Photologue uses Django generics, so I decided to see if I could use Jinja2 with generics, and then only modify the templates. It was …</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 redistributableFrequently used tags/filters for Jinja2
2008-09-17T08:48:55.547004-05:00mathwizardhttps://djangosnippets.org/snippets/1062/<p>Some frequently used filters and global functions:</p>
<p><strong>url</strong> - same as django url tag</p>
<p><strong>nbspize</strong> - replace all spaces with nbsp</p>
<p><strong>get_lang</strong> - get current language code</p>
<p><strong>timesince</strong> - converted django timesince tag</p>
<p><strong>timeuntil</strong> - converted django timeuntil tag</p>
<p><strong>truncate</strong> - tag that truncates text call it with an str …</p>
Freely redistributableJinja2 Django integration
2008-09-17T08:43:49.992403-05:00mathwizardhttps://djangosnippets.org/snippets/1061/<p>Integration of django and Jinja2.</p>
<p>Just import render_to_response from this file and you are ready.</p>
<p>This file automatically adds template search path from yout TEMPLATE_DIRS and from each installed application.</p>
<p>You can also use several variables in your settings.py to add filters, tests and global functions to the default context. …</p>
Freely redistributable