module documentation

Undocumented

Constant F Undocumented
Variable concat Undocumented
Variable internal​_code Undocumented
Variable missing Undocumented
Class _​Pass​Arg Undocumented
Class ​Cycler Cycle through values by yield them one at a time, then restarting once the end is reached. Available as cycler in templates.
Class ​Joiner A joining helper for templates.
Class ​LRUCache A simple LRU Cache implementation.
Class ​Markup Undocumented
Class ​Namespace A namespace object that can hold arbitrary attributes. It may be initialized from a dictionary or with keyword arguments.
Function clear​_caches No summary
Function consume Consumes an iterable without doing anything with it.
Function contextfunction Pass the context as the first argument to the decorated function.
Function environmentfunction Pass the environment as the first argument to the decorated function.
Function escape Undocumented
Function evalcontextfunction Pass the eval context as the first argument to the decorated function.
Function generate​_lorem​_ipsum Generate some lorem ipsum for the template.
Function htmlsafe​_json​_dumps Serialize an object to a string of JSON with json.dumps, then replace HTML-unsafe characters with Unicode escapes and mark the result safe with ~markupsafe.Markup.
Function import​_string No summary
Function internalcode Marks the function as internally used
Function is​_undefined No summary
Function object​_type​_repr Returns the name of the object's type. For some recognized singletons the name of the object is returned instead. (For example for None and Ellipsis).
Function open​_if​_exists Returns a file descriptor for the filename if that file exists, otherwise None.
Function pass​_context Pass the ~jinja2.runtime.Context as the first argument to the decorated function when called while rendering a template.
Function pass​_environment Pass the ~jinja2.Environment as the first argument to the decorated function when called while rendering a template.
Function pass​_eval​_context Pass the ~jinja2.nodes.EvalContext as the first argument to the decorated function when called while rendering a template. See :ref:`eval-context`.
Function pformat Format an object using pprint.pformat.
Function select​_autoescape Intelligently sets the initial value of autoescaping based on the filename of the template. This is the recommended way to configure autoescaping if you do not want to write a custom function yourself.
Function unicode​_urlencode Undocumented
Function url​_quote Quote a string for use in a URL using the given charset.
Function urlize Convert URLs in text into clickable links.
Variable ​_email​_re Undocumented
Variable ​_http​_re Undocumented
F =

Undocumented

Value
t.TypeVar('F',
          bound=t.Callable[..., t.Any])
concat =

Undocumented

internal_code: t.MutableSet[CodeType] =

Undocumented

missing: t.Any =

Undocumented

def clear_caches():
Jinja keeps internal caches for environments and lexers. These are used so that Jinja doesn't have to recreate environments and lexers all the time. Normally you don't have to care about that but if you are measuring memory consumption you may want to clean the caches.
def consume(iterable):
Consumes an iterable without doing anything with it.
Parameters
iterable:t.Iterable[t.Any]Undocumented
def contextfunction(f):

Pass the context as the first argument to the decorated function.

Deprecated since version 3.0: Will be removed in Jinja 3.1. Use ~jinja2.pass_context instead.
Parameters
f:FUndocumented
Returns
FUndocumented
def environmentfunction(f):

Pass the environment as the first argument to the decorated function.

Deprecated since version 3.0: Will be removed in Jinja 3.1. Use ~jinja2.pass_environment instead.
Parameters
f:FUndocumented
Returns
FUndocumented
def escape(s):

Undocumented

Parameters
s:t.AnyUndocumented
Returns
strUndocumented
def evalcontextfunction(f):

Pass the eval context as the first argument to the decorated function.

Deprecated since version 3.0: Will be removed in Jinja 3.1. Use ~jinja2.pass_eval_context instead.
New in version 2.4.
Parameters
f:FUndocumented
Returns
FUndocumented
def generate_lorem_ipsum(n=5, html=True, min=20, max=100):
Generate some lorem ipsum for the template.
Parameters
n:intUndocumented
html:boolUndocumented
min:intUndocumented
max:intUndocumented
Returns
strUndocumented
def htmlsafe_json_dumps(obj, dumps=None, **kwargs):

Serialize an object to a string of JSON with json.dumps, then replace HTML-unsafe characters with Unicode escapes and mark the result safe with ~markupsafe.Markup.

This is available in templates as the |tojson filter.

The following characters are escaped: <, >, &, '.

The returned string is safe to render in HTML documents and <script> tags. The exception is in HTML attributes that are double quoted; either use single quotes or the |forceescape filter.

Changed in version 3.0: The dumper parameter is renamed to dumps.
New in version 2.9.
Parameters
obj:t.AnyThe object to serialize to JSON.
dumps:t.Optional[t.Callable[..., str]]The dumps function to use. Defaults to env.policies["json.dumps_function"], which defaults to json.dumps.
**kwargs:t.AnyExtra arguments to pass to dumps. Merged onto env.policies["json.dumps_kwargs"].
Returns
markupsafe.MarkupUndocumented
def import_string(import_name, silent=False):

Imports an object based on a string. This is useful if you want to use import paths as endpoints or something similar. An import path can be specified either in dotted notation (xml.sax.saxutils.escape) or with a colon as object delimiter (xml.sax.saxutils:escape).

If the silent is True the return value will be None if the import fails.

Parameters
import​_name:strUndocumented
silent:boolUndocumented
Returns
t.Anyimported object
def internalcode(f):
Marks the function as internally used
Parameters
f:FUndocumented
Returns
FUndocumented
def is_undefined(obj):

Check if the object passed is undefined. This does nothing more than performing an instance check against Undefined but looks nicer. This can be used for custom filters or tests that want to react to undefined variables. For example a custom default filter can look like this:

def default(var, default=''):
    if is_undefined(var):
        return default
    return var
Parameters
obj:t.AnyUndocumented
Returns
boolUndocumented
def object_type_repr(obj):
Returns the name of the object's type. For some recognized singletons the name of the object is returned instead. (For example for None and Ellipsis).
Parameters
obj:t.AnyUndocumented
Returns
strUndocumented
def open_if_exists(filename, mode='rb'):
Returns a file descriptor for the filename if that file exists, otherwise None.
Parameters
filename:strUndocumented
mode:strUndocumented
Returns
t.Optional[t.IO]Undocumented
def pass_context(f):

Pass the ~jinja2.runtime.Context as the first argument to the decorated function when called while rendering a template.

Can be used on functions, filters, and tests.

If only Context.eval_context is needed, use pass_eval_context. If only Context.environment is needed, use pass_environment.

New in version 3.0.0: Replaces contextfunction and contextfilter.
Parameters
f:FUndocumented
Returns
FUndocumented
def pass_environment(f):

Pass the ~jinja2.Environment as the first argument to the decorated function when called while rendering a template.

Can be used on functions, filters, and tests.

New in version 3.0.0: Replaces environmentfunction and environmentfilter.
Parameters
f:FUndocumented
Returns
FUndocumented
def pass_eval_context(f):

Pass the ~jinja2.nodes.EvalContext as the first argument to the decorated function when called while rendering a template. See :ref:`eval-context`.

Can be used on functions, filters, and tests.

If only EvalContext.environment is needed, use pass_environment.

New in version 3.0.0: Replaces evalcontextfunction and evalcontextfilter.
Parameters
f:FUndocumented
Returns
FUndocumented
def pformat(obj):
Format an object using pprint.pformat.
Parameters
obj:t.AnyUndocumented
Returns
strUndocumented
def select_autoescape(enabled_extensions=('html', 'htm', 'xml'), disabled_extensions=(), default_for_string=True, default=False):

Intelligently sets the initial value of autoescaping based on the filename of the template. This is the recommended way to configure autoescaping if you do not want to write a custom function yourself.

If you want to enable it for all templates created from strings or for all templates with .html and .xml extensions:

from jinja2 import Environment, select_autoescape
env = Environment(autoescape=select_autoescape(
    enabled_extensions=('html', 'xml'),
    default_for_string=True,
))

Example configuration to turn it on at all times except if the template ends with .txt:

from jinja2 import Environment, select_autoescape
env = Environment(autoescape=select_autoescape(
    disabled_extensions=('txt',),
    default_for_string=True,
    default=True,
))

The enabled_extensions is an iterable of all the extensions that autoescaping should be enabled for. Likewise disabled_extensions is a list of all templates it should be disabled for. If a template is loaded from a string then the default from default_for_string is used. If nothing matches then the initial value of autoescaping is set to the value of default.

For security reasons this function operates case insensitive.

New in version 2.9.
Parameters
enabled​_extensions:t.Collection[str]Undocumented
disabled​_extensions:t.Collection[str]Undocumented
default​_for​_string:boolUndocumented
default:boolUndocumented
Returns
t.Callable[[t.Optional[str]], bool]Undocumented
def unicode_urlencode(obj, charset='utf-8', for_qs=False):

Undocumented

Parameters
obj:t.AnyUndocumented
charset:strUndocumented
for​_qs:boolUndocumented
Returns
strUndocumented
def url_quote(obj, charset='utf-8', for_qs=False):
Quote a string for use in a URL using the given charset.
Parameters
obj:t.AnyString or bytes to quote. Other types are converted to string then encoded to bytes using the given charset.
charset:strEncode text to bytes using this charset.
for​_qs:boolQuote "/" and use "+" for spaces.
Returns
strUndocumented
def urlize(text, trim_url_limit=None, rel=None, target=None, extra_schemes=None):

Convert URLs in text into clickable links.

This may not recognize links in some situations. Usually, a more comprehensive formatter, such as a Markdown library, is a better choice.

Works on http://, https://, www., mailto:, and email addresses. Links with trailing punctuation (periods, commas, closing parentheses) and leading punctuation (opening parentheses) are recognized excluding the punctuation. Email addresses that include header fields are not recognized (for example, mailto:address@example.com?cc=copy@example.com).

Changed in version 3.0: The extra_schemes parameter was added.
Changed in version 3.0: Generate https:// links for URLs without a scheme.
Changed in version 3.0: The parsing rules were updated. Recognize email addresses with or without the mailto: scheme. Validate IP addresses. Ignore parentheses and brackets in more cases.
Parameters
text:strOriginal text containing URLs to link.
trim​_url​_limit:t.Optional[int]Shorten displayed URL values to this length.
rel:t.Optional[str]Add the rel attribute to links.
target:t.Optional[str]Add the target attribute to links.
extra​_schemes:t.Optional[t.Iterable[str]]Recognize URLs that start with these schemes in addition to the default behavior.
Returns
strUndocumented
_email_re =

Undocumented

_http_re =

Undocumented