Links
- Source
- Docs
- Line Statements
- Line statements can span multiple lines if there are open parentheses, braces or brackets:
- API:
High Level |
Low Level |
Meta
- Global Namespace
- Variables stored in the Environment.globals dict
are special as they are available for imported templates
too, even if they are imported without context.
- This is the place where you can put variables and
functions that should be available all the time.
- Additionally Template.globals exist that
are variables available to a specific template that are
available to all render()
calls.
- Unicode
- It is not possible to use Jinja2 to process non-Unicode data.
- Notes on Identifiers
- Filters and tests are looked up in separate namespaces
and have slightly modified identifier syntax. Filters
and tests may contain dots to group filters and tests by
topic. For example it’s perfectly valid to add a
function into the filter dict and call it
to.unicode.
- Jinja2 uses a central object called the template Environment
- Using a template loader rather then passing strings to
Template or
Environment.from_string()
has multiple advantages.
- It enables template inheritance.
- The default encoding of templates is assumed to be utf-8.
- Undefined Types
- The Context
- Loaders
- Utilities
- Writing Templates
- Extensions
- Tips and Tricks
- Bytecode Cache
jinja2.FileSystemBytecodeCache(directory=None, pattern='__jinja2_%s.cache')
jinja2.MemcachedBytecodeCache(client, prefix='jinja2/bytecode/', timeout=None)
- Loaders
- Template
render([context])
generate([context])
- Returns a unicode string generator.
stream([context])
- Returns a TemplateStream
that supports buffering multiple items (since
WSGI flushes after each item) and dumping to a
stream.
- Environment
- Gotchas
- Examples
Basics
- Blocks start and stop with
{%
and %}
- Variables start and stop with
{{
and }}
- Comments start and stop with
{#
and #}
Snippets
template.module
t = Template('{% macro foo() %}42{% endmacro %}23')
unicode(t.module)
# u'23'
t.module.foo()
# u'42'