tipfy.ext.jinja2¶
This module provides functions to render templates using the Jinja2 template engine.
See the extension wiki page.
Default configuration¶
- tipfy.ext.jinja2.default_config¶
Default configuration values for this module. Keys are:
- templates_dir: Directory for templates. Default is templates.
- templates_compiled_target: Target for compiled templates. If set, uses the loader for compiled templates when deployed. If it ends with a ‘.zip’ it will be treated as a zip file. Default is None.
- force_use_compiled: Forces the use of compiled templates even in the development server.
- engine_factory: A function to be called when the template engine is instantiated, as a string. If None, uses create_jinja2_instance().
Functions¶
- tipfy.ext.jinja2.render_template(filename, **context)¶
Renders a template.
Parameters: - filename – The template filename, related to the templates directory.
- context – Keyword arguments used as variables in the rendered template.
Returns: A rendered template, in unicode.
- tipfy.ext.jinja2.render_response(filename, **context)¶
Renders a template and returns a response object.
Parameters: - filename – The template filename, related to the templates directory.
- context – Keyword arguments used as variables in the rendered template.
Returns: A tipfy.Response object with the rendered template.
- tipfy.ext.jinja2.get_template_attribute(filename, attribute)¶
Loads a macro (or variable) a template exports. This can be used to invoke a macro from within Python code. If you for example have a template named _foo.html with the following contents:
{% macro hello(name) %}Hello {{ name }}!{% endmacro %}
You can access this from Python code like this:
hello = get_template_attribute('_foo.html', 'hello') return hello('World')
This function comes from Flask.
Parameters: - filename – The template filename.
- attribute – The name of the variable of macro to acccess.
- tipfy.ext.jinja2.get_env()¶
Returns an instance of Jinja2, registering it in the WSGI app if not yet registered.
Returns: An instance of Jinja2.
