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