编写扩展

你可以编写扩展来向 Jinja2 中添加自定义标签。这是一个不平凡的任务,而且通常不需要,因为默认的标签和表达式涵盖了所有常用情况。如 i18n 扩展是一个扩展有用的好例子,而另一个会是碎片缓存。

当你编写扩展时,你需要记住你在与 Jinja2 模板编译器一同工作,而它并不验证你传递到它的节点树。如果 AST 是畸形的,你会得到各种各样的编译器或运行时错误,这调试起来极其可怕。始终确保你在使用创建正确的节点。下面的 API 文档展示了有什么节点和如何使用它们。

示例扩展

下面的例子用 Werkzeug 的缓存 contrib 模块为 Jinja2 实现了一个 cache 标签:

  1. from jinja2 import nodes
  2. from jinja2.ext import Extension
  3. class FragmentCacheExtension(Extension):
  4. # a set of names that trigger the extension.
  5. tags = set(['cache'])
  6. def __init__(self, environment):
  7. super(FragmentCacheExtension, self).__init__(environment)
  8. # add the defaults to the environment
  9. environment.extend(
  10. fragment_cache_prefix='',
  11. fragment_cache=None
  12. )
  13. def parse(self, parser):
  14. # the first token is the token that started the tag. In our case
  15. # we only listen to ``'cache'`` so this will be a name token with
  16. # `cache` as value. We get the line number so that we can give
  17. # that line number to the nodes we create by hand.
  18. lineno = parser.stream.next().lineno
  19. # now we parse a single expression that is used as cache key.
  20. args = [parser.parse_expression()]
  21. # if there is a comma, the user provided a timeout. If not use
  22. # None as second parameter.
  23. if parser.stream.skip_if('comma'):
  24. args.append(parser.parse_expression())
  25. else:
  26. args.append(nodes.Const(None))
  27. # now we parse the body of the cache block up to `endcache` and
  28. # drop the needle (which would always be `endcache` in that case)
  29. body = parser.parse_statements(['name:endcache'], drop_needle=True)
  30. # now return a `CallBlock` node that calls our _cache_support
  31. # helper method on this extension.
  32. return nodes.CallBlock(self.call_method('_cache_support', args),
  33. [], [], body).set_lineno(lineno)
  34. def _cache_support(self, name, timeout, caller):
  35. """Helper callback."""
  36. key = self.environment.fragment_cache_prefix + name
  37. # try to load the block from the cache
  38. # if there is no fragment in the cache, render it and store
  39. # it in the cache.
  40. rv = self.environment.fragment_cache.get(key)
  41. if rv is not None:
  42. return rv
  43. rv = caller()
  44. self.environment.fragment_cache.add(key, rv, timeout)
  45. return rv

而这是你在环境中使用它的方式:

  1. from jinja2 import Environment
  2. from werkzeug.contrib.cache import SimpleCache
  3. env = Environment(extensions=[FragmentCacheExtension])
  4. env.fragment_cache = SimpleCache()

之后,在模板中可以标记块为可缓存的。下面的例子缓存一个边栏 300 秒:

  1. {% cache 'sidebar', 300 %}
  2. <div class="sidebar">
  3. ...
  4. </div>
  5. {% endcache %}

扩展 API

扩展总是继承 jinja2.ext.Extension 类:

  • class _jinja2.ext.Extension(_environment)
  • Extensions can be used to add extra functionality to the Jinja templatesystem at the parser level. Custom extensions are bound to an environmentbut may not store environment specific data on self. The reason forthis is that an extension can be bound to another environment (foroverlays) by creating a copy and reassigning the environment attribute.

As extensions are created by the environment they cannot accept anyarguments for configuration. One may want to work around that by usinga factory function, but that is not possible as extensions are identifiedby their import name. The correct way to configure the extension isstoring the configuration values on the environment. Because this way theenvironment ends up acting as central configuration storage theattributes may clash which is why extensions have to ensure that the namesthey choose for configuration are not too generic. prefix for exampleis a terrible name, fragment_cache_prefix on the other hand is a goodname as includes the name of the extension (fragment cache).

  • identifier
  • 扩展的标识符。这始终是扩展类的真实导入名,不能被修改。

  • tags

  • 如果扩展实现自定义标签,这是扩展监听的标签名的集合。

  • attr(name, lineno=None)

  • Return an attribute node for the current extension. This is usefulto pass constants on extensions to generated template code.
  1. self.attr('_my_attribute', lineno=lineno)
  • callmethod(_name, args=None, kwargs=None, dyn_args=None, dyn_kwargs=None, lineno=None)
  • Call a method of the extension. This is a shortcut forattr() + jinja2.nodes.Call.

  • filterstream(_stream)

  • It’s passed a TokenStream that can be usedto filter tokens returned. This method has to return an iterable ofTokens, but it doesn’t have to return aTokenStream.

In the ext folder of the Jinja2 source distribution there is a filecalled inlinegettext.py which implements a filter that utilizes thismethod.

  • parse(parser)
  • If any of the tags matched this method is called with theparser as first argument. The token the parser stream is pointing atis the name token that matched. This method has to return one or alist of multiple nodes.

  • preprocess(source, name, filename=None)

  • This method is called before the actual lexing and can be used topreprocess the source. The filename is optional. The return valuemust be the preprocessed source.

解析器 API

传递到 Extension.parse() 的解析器提供解析不同类型表达式的方式。下面的方法可能会在扩展中使用:

  • class _jinja2.parser.Parser(_environment, source, name=None, filename=None, state=None)
  • This is the central parsing class Jinja2 uses. It’s passed toextensions and can be used to parse expressions or statements.

    • filename
    • 解析器处理的模板文件名。这 不是 模板的加载名。加载名见name 。对于不是从文件系统中加载的模板,这个值为 None

    • name

    • 模板的加载名。

    • stream

    • 当前的 TokenStream

    • fail(msg, lineno=None, exc=)

    • Convenience method that raises exc with the message, passedline number or last line number as well as the current name andfilename.

    • freeidentifier(_lineno=None)

    • Return a new free identifier as InternalName.

    • parseassign_target(_with_tuple=True, name_only=False, extra_end_rules=None)

    • Parse an assignment target. As Jinja2 allows assignments totuples, this function can parse all allowed assignment targets. Perdefault assignments to tuples are parsed, that can be disable howeverby setting with_tuple to False. If only assignments to names arewanted name_only can be set to True. The _extra_end_rules_parameter is forwarded to the tuple parsing function.

    • parseexpression(_with_condexpr=True)

    • Parse an expression. Per default all expressions are parsed, ifthe optional with_condexpr parameter is set to False conditionalexpressions are not parsed.

    • parsestatements(_end_tokens, drop_needle=False)

    • Parse multiple statements into a list until one of the end tokensis reached. This is used to parse the body of statements as it alsoparses template data if appropriate. The parser checks first if thecurrent token is a colon and skips it if there is one. Then it checksfor the block end and parses until if one of the end_tokens isreached. Per default the active token in the stream at the end ofthe call is the matched end token. If this is not wanted drop_needle_can be set to _True and the end token is removed.

    • parsetuple(_simplified=False, with_condexpr=True, extra_end_rules=None, explicit_parentheses=False)

    • Works like parse_expression but if multiple expressions aredelimited by a comma a Tuple node is created.This method could also return a regular expression instead of a tupleif no commas where found.

The default parsing mode is a full tuple. If simplified is True_only names and literals are parsed. The _no_condexpr parameter isforwarded to parse_expression().

Because tuples do not require delimiters and may end in a bogus commaan extra hint is needed that marks the end of a tuple. For examplefor loops support tuples between for and in. In that case theextra_end_rules is set to ['name:in'].

explicit_parentheses is true if the parsing was triggered by anexpression in parentheses. This is used to figure out if an emptytuple is a valid expression or not.

  • class _jinja2.lexer.TokenStream(_generator, name, filename)
  • A token stream is an iterable that yields Tokens. Theparser however does not iterate over it but calls next() to goone token ahead. The current active token is stored as current.

    • current
    • 当前的 Token

    • eos

    • Are we at the end of the stream?

    • expect(expr)

    • Expect a given token type and return it. This accepts the sameargument as jinja2.lexer.Token.test().

    • look()

    • Look at the next token.

    • next()

    • Go one token ahead and return the old one

    • nextif(_expr)

    • Perform the token test and return the token if it matched.Otherwise the return value is None.

    • push(token)

    • Push a token back to the stream.

    • skip(n=1)

    • Got n tokens ahead.

    • skipif(_expr)

    • Like nextif() but only returns _True or False.
  • _class _jinja2.lexer.Token
  • Token class.

    • lineno
    • token 的行号。

    • type

    • token 的类型。这个值是被禁锢的,所以你可以用 is 运算符同任意字符串比较。

    • value

    • token 的值。

    • test(expr)

    • Test a token against a token expression. This can either be atoken type or 'token_type:token_value'. This can only testagainst string values and types.

    • testany(*iterable_)

    • Test against multiple token expressions.

同样,在词法分析模块中也有一个实用函数可以计算字符串中的换行符数目:

  1. .. autofunction:: jinja2.lexer.count_newlines

AST

AST(抽象语法树: Abstract Syntax Tree)用于表示解析后的模板。它有编译器之后转换到可执行的 Python 代码对象的节点构建。提供自定义语句的扩展可以返回执行自定义 Python 代码的节点。

下面的清单展示了所有当前可用的节点。 AST 在 Jinja2 的各个版本中有差异,但会向后兼容。

更多信息请见 jinja2.Environment.parse()

  • _class _jinja2.nodes.Node
  • Baseclass for all Jinja2 nodes. There are a number of nodes availableof different types. There are four major types:

    • Stmt: statements
    • Expr: expressions
    • Helper: helper nodes
    • Template: the outermost wrapper nodeAll nodes have fields and attributes. Fields may be other nodes, lists,or arbitrary values. Fields are passed to the constructor as regularpositional arguments, attributes as keyword arguments. Each node hastwo attributes: lineno (the line number of the node) and environment.The environment attribute is set at the end of the parsing process forall nodes automatically.

    • find(node_type)

    • Find the first node of a given type. If no such node exists thereturn value is None.

    • findall(_node_type)

    • Find all the nodes of a given type. If the type is a tuple,the check is performed for any of the tuple items.

    • iterchild_nodes(_exclude=None, only=None)

    • Iterates over all direct child nodes of the node. This iteratesover all fields and yields the values of they are nodes. If the valueof a field is a list all the nodes in that list are returned.

    • iterfields(_exclude=None, only=None)

    • This method iterates over all fields that are defined and yields(key,value) tuples. Per default all fields are returned, butit’s possible to limit that to some fields by providing the only_parameter or to exclude some using the _exclude parameter. Bothshould be sets or tuples of field names.

    • setctx(_ctx)

    • Reset the context of a node and all child nodes. Per default theparser will all generate nodes that have a ‘load’ context as it’s themost common one. This method is used in the parser to set assignmenttargets and other nodes to a store context.

    • setenvironment(_environment)

    • Set the environment for all nodes.

    • setlineno(_lineno, override=False)

    • Set the line numbers of the node and children.
  • _class _jinja2.nodes.Expr
  • Baseclass for all expressions.

Node type:Node

  • asconst(_eval_ctx=None)
  • Return the value of the expression as constant or raiseImpossible if this was not possible.

An EvalContext can be provided, if none is givena default context is created which requires the nodes to havean attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

  • can_assign()
  • Check if it’s possible to assign something to this node.
  • class _jinja2.nodes.BinExpr(_left, right)
  • Baseclass for all binary expressions.

Node type:Expr

  • class _jinja2.nodes.Add(_left, right)
  • Add the left to the right node.

Node type:BinExpr

  • class _jinja2.nodes.And(_left, right)
  • Short circuited AND.

Node type:BinExpr

  • class _jinja2.nodes.Div(_left, right)
  • Divides the left by the right node.

Node type:BinExpr

  • class _jinja2.nodes.FloorDiv(_left, right)
  • Divides the left by the right node and truncates conver theresult into an integer by truncating.

Node type:BinExpr

  • class _jinja2.nodes.Mod(_left, right)
  • Left modulo right.

Node type:BinExpr

  • class _jinja2.nodes.Mul(_left, right)
  • Multiplies the left with the right node.

Node type:BinExpr

  • class _jinja2.nodes.Or(_left, right)
  • Short circuited OR.

Node type:BinExpr

  • class _jinja2.nodes.Pow(_left, right)
  • Left to the power of right.

Node type:BinExpr

  • class _jinja2.nodes.Sub(_left, right)
  • Substract the right from the left node.

Node type:BinExpr

  • class _jinja2.nodes.Call(_node, args, kwargs, dyn_args, dyn_kwargs)
  • Calls an expression. args is a list of arguments, kwargs a listof keyword arguments (list of Keyword nodes), and dyn_args_and _dyn_kwargs has to be either None or a node that is used asnode for dynamic positional (args) or keyword (*kwargs)arguments.

Node type:Expr

  • class _jinja2.nodes.Compare(_expr, ops)
  • Compares an expression with some other expressions. ops must be alist of Operands.

Node type:Expr

  • class _jinja2.nodes.Concat(_nodes)
  • Concatenates the list of expressions provided after converting them tounicode.

Node type:Expr

  • class _jinja2.nodes.CondExpr(_test, expr1, expr2)
  • A conditional expression (inline if expression). ({{fooifbarelsebaz}})

Node type:Expr

  • _class _jinja2.nodes.ContextReference
  • Returns the current template context. It can be used like aName node, with a 'load' ctx and will return thecurrent Context object.

Here an example that assigns the current template name to avariable named foo:

  1. Assign(Name('foo', ctx='store'),
  2. Getattr(ContextReference(), 'name'))

Node type:Expr

  • class _jinja2.nodes.EnvironmentAttribute(_name)
  • Loads an attribute from the environment object. This is useful forextensions that want to call a callback stored on the environment.

Node type:Expr

  • class _jinja2.nodes.ExtensionAttribute(_identifier, name)
  • Returns the attribute of an extension bound to the environment.The identifier is the identifier of the Extension.

This node is usually constructed by calling theattr() method on an extension.

Node type:Expr

  • class _jinja2.nodes.Filter(_node, name, args, kwargs, dyn_args, dyn_kwargs)
  • This node applies a filter on an expression. name is the name ofthe filter, the rest of the fields are the same as for Call.

If the node of a filter is None the contents of the last buffer arefiltered. Buffers are created by macros and filter blocks.

Node type:Expr

  • class _jinja2.nodes.Getattr(_node, attr, ctx)
  • Get an attribute or item from an expression that is a ascii-onlybytestring and prefer the attribute.

Node type:Expr

  • class _jinja2.nodes.Getitem(_node, arg, ctx)
  • Get an attribute or item from an expression and prefer the item.

Node type:Expr

  • class _jinja2.nodes.ImportedName(_importname)
  • If created with an import name the import name is returned on nodeaccess. For example ImportedName('cgi.escape') returns the _escape_function from the cgi module on evaluation. Imports are optimized by thecompiler so there is no need to assign them to local variables.

Node type:Expr

  • class _jinja2.nodes.InternalName(_name)
  • An internal name in the compiler. You cannot create these nodesyourself but the parser provides afree_identifier() method that createsa new identifier for you. This identifier is not available from thetemplate and is not threated specially by the compiler.

Node type:Expr

  • _class _jinja2.nodes.Literal
  • Baseclass for literals.

Node type:Expr

  • class _jinja2.nodes.Const(_value)
  • All constant values. The parser will return this node for simpleconstants such as 42 or "foo" but it can be used to store morecomplex values such as lists too. Only constants with a saferepresentation (objects where eval(repr(x))==x is true).

Node type:Literal

  • class _jinja2.nodes.Dict(_items)
  • Any dict literal such as {1:2,3:4}. The items must be a list ofPair nodes.

Node type:Literal

  • class _jinja2.nodes.List(_items)
  • Any list literal such as [1,2,3]

Node type:Literal

  • class _jinja2.nodes.TemplateData(_data)
  • A constant template string.

Node type:Literal

  • class _jinja2.nodes.Tuple(_items, ctx)
  • For loop unpacking and some other things like multiple argumentsfor subscripts. Like for Namectx specifies if the tupleis used for loading the names or storing.

Node type:Literal

  • class _jinja2.nodes.MarkSafe(_expr)
  • Mark the wrapped expression as safe (wrap it as Markup).

Node type:Expr

  • class _jinja2.nodes.MarkSafeIfAutoescape(_expr)
  • Mark the wrapped expression as safe (wrap it as Markup) butonly if autoescaping is active.

New in version 2.5.

Node type:Expr

  • class _jinja2.nodes.Name(_name, ctx)
  • Looks up a name or stores a value in a name.The ctx of the node can be one of the following values:

    • store: store a value in the name
    • load: load that name
    • param: like store but if the name was defined as function parameter.

Node type:Expr

  • class _jinja2.nodes.Slice(_start, stop, step)
  • Represents a slice object. This must only be used as argument forSubscript.

Node type:Expr

  • class _jinja2.nodes.Test(_node, name, args, kwargs, dyn_args, dyn_kwargs)
  • Applies a test on an expression. name is the name of the test, therest of the fields are the same as for Call.

Node type:Expr

  • class _jinja2.nodes.UnaryExpr(_node)
  • Baseclass for all unary expressions.

Node type:Expr

  • class _jinja2.nodes.Neg(_node)
  • Make the expression negative.

Node type:UnaryExpr

  • class _jinja2.nodes.Not(_node)
  • Negate the expression.

Node type:UnaryExpr

  • class _jinja2.nodes.Pos(_node)
  • Make the expression positive (noop for most expressions)

Node type:UnaryExpr

  • _class _jinja2.nodes.Helper
  • Nodes that exist in a specific context only.

Node type:Node

  • class _jinja2.nodes.Keyword(_key, value)
  • A key, value pair for keyword arguments where key is a string.

Node type:Helper

  • class _jinja2.nodes.Operand(_op, expr)
  • Holds an operator and an expression.The following operators are available: %, *, , +, -, //, /, eq, gt, gteq, in, lt, lteq, ne, not, notin

Node type:Helper

  • class _jinja2.nodes.Pair(_key, value)
  • A key, value pair for dicts.

Node type:Helper

  • _class _jinja2.nodes.Stmt
  • Base node for all statements.

Node type:Node

  • class _jinja2.nodes.Assign(_target, node)
  • Assigns an expression to a target.

Node type:Stmt

  • class _jinja2.nodes.Block(_name, body, scoped)
  • A node that represents a block.

Node type:Stmt

  • _class _jinja2.nodes.Break
  • Break a loop.

Node type:Stmt

  • class _jinja2.nodes.CallBlock(_call, args, defaults, body)
  • Like a macro without a name but a call instead. call is called withthe unnamed macro as caller argument this node holds.

Node type:Stmt

  • _class _jinja2.nodes.Continue
  • Continue a loop.

Node type:Stmt

  • class _jinja2.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:

  1. EvalContextModifier(options=[Keyword('autoescape', Const(True))])

Node type:Stmt

  • class _jinja2.nodes.ScopedEvalContextModifier(_options, body)
  • Modifies the eval context and reverts it later. Works exactly likeEvalContextModifier but will only modify theEvalContext for nodes in the body.

Node type:EvalContextModifier

  • class _jinja2.nodes.ExprStmt(_node)
  • A statement that evaluates an expression and discards the result.

Node type:Stmt

  • class _jinja2.nodes.Extends(_template)
  • Represents an extends statement.

Node type:Stmt

  • class _jinja2.nodes.FilterBlock(_body, filter)
  • Node for filter sections.

Node type:Stmt

  • class _jinja2.nodes.For(_target, iter, body, else__, _test, recursive)
  • The for loop. target is the target for the iteration (usually aName or Tuple), iter the iterable. body is a listof 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

  • class _jinja2.nodes.FromImport(_template, names, with_context)
  • A node that represents the from import tag. It’s important to notpass unsafe names to the name attribute. The compiler translates theattribute lookups directly into getattr calls and does not use thesubscript callback of the interface. As exported variables may notstart with double underscores (which the parser asserts) this is not aproblem for regular Jinja code, but if this node is used in an extensionextra care must be taken.

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

Node type:Stmt

  • class _jinja2.nodes.If(_test, body, else_)
  • If test is true, body is rendered, else else_.

Node type:Stmt

  • class _jinja2.nodes.Import(_template, target, with_context)
  • A node that represents the import tag.

Node type:Stmt

  • class _jinja2.nodes.Include(_template, with_context, ignore_missing)
  • A node that represents the include tag.

Node type:Stmt

  • class _jinja2.nodes.Macro(_name, args, defaults, body)
  • A macro definition. name is the name of the macro, args a list ofarguments and defaults a list of defaults if there are any. body isa list of nodes for the macro body.

Node type:Stmt

  • class _jinja2.nodes.Output(_nodes)
  • A node that holds multiple expressions which are then printed out.This is used both for the print statement and the regular template data.

Node type:Stmt

  • class _jinja2.nodes.Scope(_body)
  • An artificial scope.

Node type:Stmt

  • class _jinja2.nodes.Template(_body)
  • Node that represents a template. This must be the outermost node thatis passed to the compiler.

Node type:Node

  • _exception _jinja2.nodes.Impossible
  • Raised if the node could not perform a requested action.