jinja2.sandbox.Environment
¶
-
class
jinja2.sandbox.
Environment
(block_start_string='{%', block_end_string='%}', variable_start_string='{{', variable_end_string='}}', comment_start_string='{#', comment_end_string='#}', line_statement_prefix=None, line_comment_prefix=None, trim_blocks=False, lstrip_blocks=False, newline_sequence='n', keep_trailing_newline=False, extensions=(), optimized=True, undefined=<class 'jinja2.runtime.Undefined'>, finalize=None, autoescape=False, loader=None, cache_size=400, auto_reload=True, bytecode_cache=None)[source]¶ The core component of Jinja is the Environment. It contains important shared variables like configuration, filters, tests, globals and others. Instances of this class may be modified if they are not shared and if no template was loaded so far. Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior.
Here are the possible initialization parameters:
- block_start_string
- The string marking the beginning of a block. Defaults to
'{%'
. - block_end_string
- The string marking the end of a block. Defaults to
'%}'
. - variable_start_string
- The string marking the beginning of a print statement.
Defaults to
'{{'
. - variable_end_string
- The string marking the end of a print statement. Defaults to
'}}'
. - comment_start_string
- The string marking the beginning of a comment. Defaults to
'{#'
. - comment_end_string
- The string marking the end of a comment. Defaults to
'#}'
. - line_statement_prefix
- If given and a string, this will be used as prefix for line based statements. See also Line Statements.
- line_comment_prefix
If given and a string, this will be used as prefix for line based comments. See also Line Statements.
New in version 2.2.
- trim_blocks
- If this is set to
True
the first newline after a block is removed (block, not variable tag!). Defaults to False. - lstrip_blocks
- If this is set to
True
leading spaces and tabs are stripped from the start of a line to a block. Defaults to False. - newline_sequence
- The sequence that starts a newline. Must be one of
'\r'
,'\n'
or'\r\n'
. The default is'\n'
which is a useful default for Linux and OS X systems as well as web applications. - keep_trailing_newline
Preserve the trailing newline when rendering templates. The default is
False
, which causes a single newline, if present, to be stripped from the end of the template.New in version 2.7.
- extensions
- List of Jinja extensions to use. This can either be import paths as strings or extension classes. For more information have a look at the extensions documentation.
- optimized
- should the optimizer be enabled? Default is True.
- undefined
Undefined
or a subclass of it that is used to represent undefined values in the template.- finalize
- A callable that can be used to process the result of a variable expression before it is output. For example one can convert None implicitly into an empty string here.
- autoescape
If set to true the XML/HTML autoescaping feature is enabled by default. For more details about autoescaping see
Markup
. As of Jinja 2.4 this can also be a callable that is passed the template name and has to return True or False depending on autoescape should be enabled by default.Changed in version 2.4: autoescape can now be a function
- loader
- The template loader for this environment.
- cache_size
The size of the cache. Per default this is
400
which means that if more than 400 templates are loaded the loader will clean out the least recently used template. If the cache size is set to0
templates are recompiled all the time, if the cache size is-1
the cache will not be cleaned.Changed in version 2.8: The cache size was increased to 400 from a low 50.
- auto_reload
- Some loaders load templates from locations where the template sources may change (ie: file system or database). If auto_reload is set to True (default) every time a template is requested the loader checks if the source changed and if yes, it will reload the template. For higher performance it’s possible to disable that.
- bytecode_cache
If set to a bytecode cache object, this object will provide a cache for the internal Jinja bytecode so that templates don’t have to be parsed if they were not changed.
See Bytecode Cache for more information.
Methods¶
__init__ ([block_start_string, ...]) |
|
add_extension (extension) |
Adds an extension after the environment was created. |
call_filter (name, value[, args, kwargs, ...]) |
Invokes a filter on a value the same way the compiler does it. |
call_test (name, value[, args, kwargs]) |
Invokes a test on a value the same way the compiler does it. |
compile (source[, name, filename, raw, ...]) |
Compile a node or template source code. |
compile_expression (source[, undefined_to_none]) |
A handy helper method that returns a callable that accepts keyword arguments that appear as variables in the expression. |
compile_templates (target[, extensions, ...]) |
Finds all the templates the loader can find, compiles them and stores them in target. |
extend (**attributes) |
Add the items to the instance of the environment if they do not exist yet. |
from_string (source[, globals, template_class]) |
Load a template from a string. |
get_or_select_template (template_name_or_list) |
Does a typecheck and dispatches to select_template() if an iterable of template names is given, otherwise to get_template() . |
get_template (name[, parent, globals]) |
Load a template from the loader. |
getattr (obj, attribute) |
Get an item or attribute of an object but prefer the attribute. |
getitem (obj, argument) |
Get an item or attribute of an object but prefer the item. |
handle_exception ([exc_info, rendered, ...]) |
Exception handling helper. |
iter_extensions () |
Iterates over the extensions by priority. |
join_path (template, parent) |
Join a template with the parent. |
lex (source[, name, filename]) |
Lex the given sourcecode and return a generator that yields tokens as tuples in the form (lineno, token_type, value) . |
list_templates ([extensions, filter_func]) |
Returns a list of templates for this environment. |
make_globals (d) |
Return a dict for the globals. |
overlay ([block_start_string, ...]) |
Create a new overlay environment that shares all the data with the current environment except for cache and the overridden attributes. |
parse (source[, name, filename]) |
Parse the sourcecode and return the abstract syntax tree. |
preprocess (source[, name, filename]) |
Preprocesses the source with all extensions. |
select_template (names[, parent, globals]) |
Works like get_template() but tries a number of templates before it fails. |
Attributes¶
exception_formatter |
|
exception_handler |
these are currently EXPERIMENTAL undocumented features. |
lexer |
The lexer for this environment. |
linked_to |
the environment this environment is linked to if it is an overlay |
overlayed |
True if the environment is just an overlay |
sandboxed |
if this environment is sandboxed. Modifying this variable won’t make |
shared |
shared environments have this set to True. A shared environment |