Snippet List
This is a minimal template loader for Django 1.2 or higher that loads [Jinja2](http://jinja.pocoo.org/2/) templates. It is better integrated with Django than using Jinja2 directly:
* Your view code is the same
* Unmodified generic views use it
* RequestContext and context processors still work
To use it, add the following to you `settings.py` file:
TEMPLATE_LOADERS = (
'jinja2_for_django.Loader',
)
It searches for templates in the same places as `django.template.loaders.app_directories.Loader` − that is in the `templates` directory of each installed app.
Django custom and default template tags and filters are not available. Some are the same in Jinja2, but you need to replace the others yourself. You can add global filters and variables (such as functions) in the `Loader.env.filters` and `Loader.env.globals` dicts. You can not add tags. See the [Jinja2 documentation](http://jinja.pocoo.org/2/documentation/) for more details.
- template
- jinja
- jinja2
- django1.2
init env
`env = Envoriment(extensions=('youproject.app.extensions.csrf_token'), loader=loader)`
or see [http://www.djangosnippets.org/snippets/1844/] and in settings.py:
`JINJA_EXTS=('jinja2.ext.i18n','youproject.app.extensions.csrf_token',)`
use this extension in jinja2 template just like django template:
`<form ...>{% csrf_token %}...</form>`
I tried a few snippets of integrated jinja2 in django, which provided ?.render_to_string and ?.render_to_response in the way of jinja2. **But those snippets could not use the generic view**, because of the generic views is use default django template. so i write this snippet which could use generic view, and use the basic django.shortcuts.render_to_string, django.shortcuts.render_to_string.
#in yourproject/urls.py :
#install default environment is very simple
djangojinja2.install()
#install environment of specified Environment class
from jinja2.sandbox import SandboxedEnvironment
djangojinja2.install(SandboxedEnvironment)
#alternative you can install sepcified environment
env=Environment(...)
...
djangojinja2.install(env)
#something could be set in settings.py
TEMPLATE_DIRS = (path.join(path.dirname(__file__),"templates"),)
JINJA_GLOBALS=['myapp.myutil.foo',]
JINJA_FILTERS=['django.template.defaultfilters.date',]
JINJA_TESTS=('foo.mytest',)
JINJA_EXTS=['jinja2.ext.i18n']
#there is no change need for app.views
from django.shortcuts import render_to_response
def foo(request):
return render_to_response('/myjinja2.html',{'request':request})
test in django development version of r12026 , jinja2 2.2.1, python 2.5
- template
- generic
- jinja2
- generic-view
This is heavily inspired by [http://code.google.com/p/smorgasbord/](http://code.google.com/p/smorgasbord/). But that couldn't reuse an existing jinja2 Environment, nor set filters on the Environment it created.
This code assumes that you have `env` declared previously in the file as your Jinja2 Environment instance.
In `settings.py`, you should set
KEEP_DJANGO_TEMPLATES = (
'/django/contrib/',
)
so that your Django admin still works. You can also set any other places that you do want to use Django templates there.
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 a bit of work, but I seem to have done it. Django generics can take template_loader as an option, so if you have the same interface, things should just work.
The template must accept RequestContext as an argument to render(), so here we subclass jinja2.Template and when it receives Django's RequestContext object, it creates a flat dictionary from it, which jinja2 can work with.
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).
In Jinja, you don't need costum tags (most of the time), because you can call functions and pass them parameters too!
The only problem is that you cannot "load" functions from the template, this "loading" must be done by the code that renders the template. Same goes for filters and tests.
If you need only two or three functions/filters, no problem, you can manually add them to the Environment object; but this method doesn't really scale with real-life webapps.
This module/snippet offers a solution by allowing the user to define application-specific functions and filters and load them into the jinja2 environment automatically.
Here's how to use this:
1. Install Jinja2 (for a quick & dirty installation, you can just put the jinja2 folder in a folder that's in PYTHONPATH)
2. Save this python module somewhere and call it something meaningful (I have it as jinja.py in my project directory)
3. Whenever you need to respond (an HttpResponse) with a template that's rendered by jinja, import this module and call `return jrespond( template_name, context )`
4. Any filters or functions you define in any of your applications will be readily available inside the template (see the documentation in code)
- template
- templates
- jinja
- template-tags
- jinja2
Some frequently used filters and global functions:
**url** - same as django url tag
**nbspize** - replace all spaces with nbsp
**get_lang** - get current language code
**timesince** - converted django timesince tag
**timeuntil** - converted django timeuntil tag
**truncate** - tag that truncates text call it with an str argument like '20c' or '5w', where the number provides the count and c stands for charachters and w stands for words
Integration of django and Jinja2.
Just import render_to_response from this file and you are ready.
This file automatically adds template search path from yout TEMPLATE_DIRS and from each installed application.
You can also use several variables in your settings.py to add filters, tests and global functions to the default context. This can be done by using JINJA_GLOBALS, JINJA_FILTERS and JINJA_TEST e.g.
`JINJA_FILTERS = (
'myapp.filters.myfilter',
'myapp.filters.myfilter2',
)`
9 snippets posted so far.