找回密码
 立即注册

扩展

2022-2-22 23:50| 发布者: 笨鸟自学网| 查看: 7569| 评论: 0

摘要: Jinja2 支持扩展来添加过滤器、测试、全局变量或者甚至是处理器。扩展的主要动力是 把诸如添加国际化支持的常用代码迁移到一个可重用的类。添加扩展扩展在 Jinja2 环境创建时被添加。一旦环境被创建,就不能添加额外 ...


classjinja2.nodes.Block(namebodyscoped)

A node that represents a block.

Node type:Stmt
classjinja2.nodes.Break

Break a loop.

Node type:Stmt
classjinja2.nodes.CallBlock(callargsdefaultsbody)

Like a macro without a name but a call instead. call is called with the unnamed macro as caller argument this node holds.

Node type:Stmt
classjinja2.nodes.Continue

Continue a loop.

Node type:Stmt
classjinja2.nodes.EvalContextModifier(options)

Modifies the eval context. For each option that should be modified, a Keyword has to be added to the options list.

Example to change the autoescape setting:

EvalContextModifier(options=[Keyword('autoescape', Const(True))])
Node type:Stmt
classjinja2.nodes.ScopedEvalContextModifier(optionsbody)

Modifies the eval context and reverts it later. Works exactly like EvalContextModifier but will only modify the EvalContext for nodes in the body.

Node type:EvalContextModifier
classjinja2.nodes.ExprStmt(node)

A statement that evaluates an expression and discards the result.

Node type:Stmt
classjinja2.nodes.Extends(template)

Represents an extends statement.

Node type:Stmt
classjinja2.nodes.FilterBlock(bodyfilter)

Node for filter sections.

Node type:Stmt
classjinja2.nodes.For(targetiterbodyelse_testrecursive)

The for loop. target is the target for the iteration (usually a Name or Tuple), iter the iterable. body is a list of nodes that are used as loop-body, and else_ a list of nodes for the else block. If no else node exists it has to be an empty list.

For filtered nodes an expression can be stored as test, otherwise None.

Node type:Stmt
classjinja2.nodes.FromImport(templatenameswith_context)

A node that represents the from import tag. It’s important to not pass unsafe names to the name attribute. The compiler translates the attribute lookups directly into getattr calls and does not use the subscript callback of the interface. As exported variables may not start with double underscores (which the parser asserts) this is not a problem for regular Jinja code, but if this node is used in an extension extra care must be taken.

The list of names may contain tuples if aliases are wanted.

Node type:Stmt
classjinja2.nodes.If(testbodyelse_)

If test is true, body is rendered, else else_.

Node type:Stmt
classjinja2.nodes.Import(templatetargetwith_context)

A node that represents the import tag.

Node type:Stmt
classjinja2.nodes.Include(templatewith_contextignore_missing)

A node that represents the include tag.

Node type:Stmt
classjinja2.nodes.Macro(nameargsdefaultsbody)

A macro definition. name is the name of the macro, args a list of arguments and defaults a list of defaults if there are any. 

上一篇:模板设计者文档下一篇:集成

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

GMT+8, 2025-8-31 01:06 , Processed in 0.030195 second(s), 18 queries .