Python jinja2.contextfunction() Examples

The following are 5 code examples of jinja2.contextfunction(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jinja2 , or try the search function .
Example #1
Source File: jinja2tags.py    From longclaw with MIT License 5 votes vote down vote up
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 
Example #2
Source File: jinja2tags.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, environment):
        super().__init__(environment)

        self.environment.globals.update({
            'wagtailuserbar': jinja2.contextfunction(wagtailuserbar),
        })


# Nicer import names 
Example #3
Source File: jinja2tags.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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,
        }) 
Example #4
Source File: unchained.py    From flask-unchained with MIT License 5 votes vote down vote up
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 
Example #5
Source File: web.py    From msys2-web with MIT License 5 votes vote down vote up
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