本文整理汇总了Python中jinja2.contextfilter方法的典型用法代码示例。如果您正苦于以下问题:Python jinja2.contextfilter方法的具体用法?Python jinja2.contextfilter怎么用?Python jinja2.contextfilter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2
的用法示例。
在下文中一共展示了jinja2.contextfilter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _make_attr_filter
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import contextfilter [as 别名]
def _make_attr_filter(attr):
@jinja2.contextfilter
def func(context, name, inventory_hostname=None):
return net_attr(context, name, attr, inventory_hostname)
return func
示例2: template_filter
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import contextfilter [as 别名]
def template_filter(self,
arg: Optional[Callable] = None,
*,
name: Optional[str] = None,
pass_context: bool = False,
inject: Optional[Union[bool, Iterable[str]]] = None,
safe: bool = False,
) -> Callable:
"""
Decorator to mark a function as a Jinja template filter.
:param name: The name of the filter, if different from the function name.
:param pass_context: Whether or not to pass the template context into the filter.
If ``True``, the first argument must be the context.
:param inject: Whether or not this filter needs any dependencies injected.
:param safe: Whether or not to mark the output of this filter as html-safe.
"""
def wrapper(fn):
fn = _inject(fn, inject)
if safe:
fn = _make_safe(fn)
if pass_context:
fn = jinja2.contextfilter(fn)
self._defer(lambda app: app.add_template_filter(fn, name=name))
return fn
if callable(arg):
return wrapper(arg)
return wrapper