Table of Contents
To use templates your controller methods return a 2-tuple or 3-tuple:
(template_name, data)
(template_name, data, template_options)
You can omit the brackets when returning from your controller.
A string in one of the following formats:
Any of the above can be prefixed with the name of an engine and a colon to override the default engine defined in your configuration.
Relative paths are relative to the working directory your web application was run within.
An example controller method which passes the name given as an argument to the controller to a template:
def hello(self, name="world"):
return "helloworld.templates.hello", dict(name=name)
The default templating language is Genshi, however there are many others available. If you want to default all of your controller methods (that use templates) to another language, define the following in your configuration:
web.templating.engine = jinja2
You can also override the templating engine on a per-template basis by prefixing the name of the templating engine to the name of the template:
"jinja2:helloworld.templates.hello"
The Common Template Interface includes a number of data serialization formats out-of-the-box. The most useful ones for web development include:
Java Script Object Notation is best for interactive sites as JavaScript can parse the data natively.
JSON serialization is included in Python 2.6; in Python 2.5 you will need to install the simplejson module and include it in your project’s install_requires.
Others include the Python marshal and pickle formats, but those should be used with caution as they can contain executable code.
To use any of these serialization formats specify the name of the format and a single colon in your returned template name with the data to serialize as the second part of the 2-tuple. E.g.:
def index(self):
return "json:", dict(name="world")
Unlike full templating languages, you are not restricted to dictionaries as your top-level container:
def index(self):
return "json:", ('name', ['bill', 'bob', 'world'])
WebCore includes a number of useful helpers in the web and global template namespaces:
Sometimes you want to render templates directly in your controller code, like when you need to use them to format an e-mail message you’re sending out. This is done using the render() function:
from web.core.templating import render
data = {'user': 'My Name', 'access_level', 'admin'}
mimetype, output = render('myproject.templates/email.html', data)
The mimetype variable contains the MIME type that resulted from the rendering of this template, which is text/html in this case. The output variable contains the actual rendered HTML as a unicode string or bytestring depending on the rendering engine used. Of course, you can also use this function to serialize data using any supported serialization format (JSON, bencode etc.).
The render() function is used internally to render templates for controllers that return a tuple, so the syntax for the template name is the same as described in the first section here. The same global variables are also available, though they will be empty if not called from within a controller (or, in other words, when the current thread is not handling a request).
To add objects to the global template namespace, append to the web.core.templating.registry dictionary:
from web.core.templating import registry
registry.append(dict(
myglobal="foo"
))
To add objects to the web namespace, extend the web.core.namespace dictionary:
import web
web.core.namespace.extend(dict(
myglobal="foo"
))