tipfy.ext.jinja2 ¶
This extension provides Jinja2 templating support to tipfy.
Usage is very simple: you simply need to import render_template or render_response
from the extension module, passing the template filename you want to render and keyword arguments
to be used in the template.
render_template()returns a string, this can be used as body for an email for example.render_response()returns a tipfy.Response object which is sent to browser.
handlers.py
from tipfy import RequestHandler
from tipfy.ext.jinja2 import render_response
class MyHandler(RequestHandler):
def get(self, **kwargs):
return render_response('hello_world.html', message='Hello, World!')
Templates are by default loaded from the templates directory from your application root.
You can change this in the configuration:
config.py
config['tipfy.ext.jinja2'] = {
'templates_dir': 'my_template_directory',
}
Jinja2Mixin¶
A convenient way to render Jinja2 templates is to make RequestHandler extend Jinja2Mixin:
handlers.py
from tipfy import RequestHandler
from tipfy.ext.jinja2 import Jinja2Mixin
class MyHandler(RequestHandler, Jinja2Mixin):
def get(self, **kwargs):
return self.render_response('hello_world.html', message='Hello, World!')
In this case, request context variables are also passed to the template. Keyword
arguments passed to self.render_response() override request context variables.
Extension Reference¶
- Name: tipfy.ext.jinja2
- Author: Rodrigo Moraes
- License: BSD
- Tags: template engine, templates
- URL: http://www.tipfy.org/wiki/extensions/jinja2/
- PyPi page: http://pypi.python.org/pypi/tipfy.ext.jinja2/
- Source code: http://code.google.com/p/tipfy-ext-jinja2/
- Issue tracker: http://code.google.com/p/tipfy-ext-jinja2/issues/list
- Version: latest
- Edited by Jeff on 8/14/10 12:37 PM
- Save on Delicious | Submit to Reddit
- History
- Edit
