Toggle navigation

Native Python Types

The default Environment renders templates to strings. With NativeEnvironment, rendering a template produces a native Python type. This is useful if you are using Jinja outside the context of creating text files. For example, your code may have an intermediate step where users may use templates to define values that will then be passed to a traditional string environment.


Adding two values results in an integer, not a string with a number:

[UNKNOWN NODE doctest_block]

Rendering list syntax produces a list:

[UNKNOWN NODE doctest_block]

Rendering something that doesn’t look like a Python literal produces a string:

[UNKNOWN NODE doctest_block]

Rendering a Python object produces that object as long as it is the only node:

[UNKNOWN NODE doctest_block]


class jinja2.nativetypes.NativeEnvironment([options])

An environment that renders templates to native Python types.

class jinja2.nativetypes.NativeTemplate([options])
render(*args, **kwargs)

Render the template to produce a native Python type. If the result is a single node, its value is returned. Otherwise, the nodes are concatenated as strings. If the result can be parsed with ast.literal_eval(), the parsed value is returned. Otherwise, the string is returned.