Utilities
These helper functions and classes are useful if you add custom filters orfunctions to a Jinja2 environment.
jinja2.
environmentfilter
(f)- Decorator for marking environment dependent filters. The current
Environment
is passed to the filter as first argument.
jinja2.
contextfilter
(f)- Decorator for marking context dependent filters. The current
Context
will be passed as first argument.
jinja2.
evalcontextfilter
(f)- Decorator for marking eval-context dependent filters. An evalcontext object is passed as first argument. For more informationabout the eval context, see Evaluation Context.
Changelog
New in version 2.4.
jinja2.
environmentfunction
(f)- This decorator can be used to mark a function or method as environmentcallable. This decorator works exactly like the
contextfunction()
decorator just that the first argument is the activeEnvironment
and not context.
jinja2.
contextfunction
(f)- This decorator can be used to mark a function or method context callable.A context callable is passed the active
Context
as first argument whencalled from the template. This is useful if a function wants to get accessto the context or functions provided on the context object. For examplea function that returns a sorted list of template variables the currenttemplate exports could look like this:
@contextfunction
def get_exported_names(context):
return sorted(context.exported_vars)
jinja2.
evalcontextfunction
(f)- This decorator can be used to mark a function or method as an evalcontext callable. This is similar to the
contextfunction()
but instead of passing the context, an evaluation context object ispassed. For more information about the eval context, seeEvaluation Context.
Changelog
New in version 2.4.
jinja2.
clear_caches
()- Jinja2 keeps internal caches for environments and lexers. These areused so that Jinja2 doesn’t have to recreate environments and lexers allthe time. Normally you don’t have to care about that but if you aremeasuring memory consumption you may want to clean the caches.
jinja2.
isundefined
(_obj)- Check if the object passed is undefined. This does nothing more thanperforming an instance check against
Undefined
but looks nicer.This can be used for custom filters or tests that want to react toundefined variables. For example a custom default filter can look likethis:
def default(var, default=''):
if is_undefined(var):
return default
return var
markupsafe.
escape
(s) → markup- Convert the characters &, <, >, ‘, and ” in string s to HTML-safesequences. Use this if you need to display text that might containsuch characters in HTML. Marks return value as markup string.
- class
markupsafe.
Markup
- A string that is ready to be safely inserted into an HTML or XMLdocument, either because it was escaped or because it was markedsafe.
Passing an object to the constructor converts it to text and wrapsit to mark it safe without escaping. To escape the text, use theescape()
class method instead.
>>> Markup('Hello, <em>World</em>!')
Markup('Hello, <em>World</em>!')
>>> Markup(42)
Markup('42')
>>> Markup.escape('Hello, <em>World</em>!')
Markup('Hello <em>World</em>!')
This implements the html()
interface that some frameworksuse. Passing an object that implements html()
will wrap theoutput of that method, marking it safe.
>>> class Foo:
... def __html__(self):
... return '<a href="/foo">foo</a>'
...
>>> Markup(Foo())
Markup('<a href="/foo">foo</a>')
This is a subclass of the text type (str
in Python 3,unicode
in Python 2). It has the same methods as that type, butall methods escape their arguments and return a Markup
instance.
>>> Markup('<em>%s</em>') % 'foo & bar'
Markup('<em>foo & bar</em>')
>>> Markup('<em>Hello</em> ') + '<foo>'
Markup('<em>Hello</em> <foo>')
- classmethod
escape
(s) Escape a string. Calls
escape()
and ensures that forsubclasses the correct type is returned.unescape()
the markup, remove tags, and normalizewhitespace to single spaces.
>>> Markup('Main » <em>About</em>').striptags()
'Main » About'
unescape
()- Convert escaped markup back into a text string. This replacesHTML entities with the characters they represent.
>>> Markup('Main » <em>About</em>').unescape()
'Main » <em>About</em>'