笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

API

发布者: 笨鸟自学网



高层 API

高层 API 即是你会在应用中用于加载并渲染模板的 API 。 低层 API 相反,只在你想深入挖掘 Jinja2 或 开发扩展 时有用。

classjinja2.Environment([options])

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_comment_prefix

If given and a string, this will be used as prefix for line based comments. See also 行语句.

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 to 0 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 字节码缓存 for more information.

shared

如果模板通过 Template 构造函数创建,会自动创建一个环境。这 些环境被创建为共享的环境,这意味着多个模板拥有相同的匿名环境。对所有 模板共享环境,这个属性为 True ,反之为 False 。

sandboxed

如果环境在沙箱中,这个属性为 True 。沙箱模式见文档中的 SandboxedEnvironment 

filters

该环境的过滤器字典。只要没有加载过模板,添加新过滤器或删除旧的都是 安全的。自定义过滤器见 自定义过滤器 。有效的过滤器名称见 标识符的说明 。

tests

该环境的测试函数字典。只要没有加载过模板,修改这个字典都是安全的。 自定义测试见 see 自定义测试 。有效的测试名见 标识符的说明 。

globals

一个全局变量字典。这些变量在模板中总是可用。只要没有加载过模板,修 改这个字典都是安全的。更多细节见 全局命名空间 。有效的 对象名见 标识符的说明 。


上一篇:介绍下一篇:沙箱

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-17 04:30 , Processed in 0.021765 second(s), 17 queries .

© 2001-2020

返回顶部