Python jinja2.contextfilter() Examples

The following are 2 code examples of jinja2.contextfilter(). 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: networks.py    From kayobe with Apache License 2.0 5 votes vote down vote up
def _make_attr_filter(attr):
    @jinja2.contextfilter
    def func(context, name, inventory_hostname=None):
        return net_attr(context, name, attr, inventory_hostname)
    return func 
Example #2
Source File: unchained.py    From flask-unchained with MIT License 5 votes vote down vote up
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