本文整理匯總了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