本文整理汇总了Python中jinja2.contextfunction方法的典型用法代码示例。如果您正苦于以下问题:Python jinja2.contextfunction方法的具体用法?Python jinja2.contextfunction怎么用?Python jinja2.contextfunction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2
的用法示例。
在下文中一共展示了jinja2.contextfunction方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import contextfunction [as 别名]
def __init__(self, environment):
super(LongClawBasketExtension, self).__init__(environment)
self.environment.globals.update({
'basket': jinja2.contextfunction(get_basket_items),
'add_to_basket_btn': add_to_basket_btn,
})
# Nicer import names
示例2: __init__
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import contextfunction [as 别名]
def __init__(self, environment):
super().__init__(environment)
self.environment.globals.update({
'wagtailuserbar': jinja2.contextfunction(wagtailuserbar),
})
# Nicer import names
示例3: __init__
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import contextfunction [as 别名]
def __init__(self, environment):
super().__init__(environment)
self.environment.globals.update({
'pageurl': jinja2.contextfunction(pageurl),
'slugurl': jinja2.contextfunction(slugurl),
'wagtail_version': wagtail_version,
})
self.environment.filters.update({
'richtext': richtext,
})
示例4: template_global
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import contextfunction [as 别名]
def template_global(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 global (tag).
:param name: The name of the tag, if different from the function name.
:param pass_context: Whether or not to pass the template context into the tag.
If ``True``, the first argument must be the context.
:param inject: Whether or not this tag needs any dependencies injected.
:param safe: Whether or not to mark the output of this tag as html-safe.
"""
def wrapper(fn):
fn = _inject(fn, inject)
if safe:
fn = _make_safe(fn)
if pass_context:
fn = jinja2.contextfunction(fn)
self._defer(lambda app: app.add_template_global(fn, name=name))
return fn
if callable(arg):
return wrapper(arg)
return wrapper
示例5: context_function
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import contextfunction [as 别名]
def context_function(name: str) -> Callable:
def wrap(f: Callable) -> Callable:
@jinja2.contextfunction
def ctxfunc(context: Dict, *args: Any, **kwargs: Any) -> Any:
return f(context["request"], *args, **kwargs)
templates.env.globals[name] = ctxfunc
return f
return wrap