A very fast and expressive template engine for Python applications
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Specialized template environments that render templates to native Python types instead of strings. Returns actual Python objects (integers, lists, dictionaries, etc.) when possible, with string fallback for complex expressions.
Environment that renders templates to native Python types using custom code generation and concatenation logic.
class NativeEnvironment(Environment):
"""
An environment that renders templates to native Python types.
Uses a specialized code generator that avoids converting values to strings,
allowing templates to return actual Python objects like integers, lists,
and dictionaries when the template output can be parsed as valid Python literals.
"""Template class that works with NativeEnvironment to produce native Python values instead of string output.
class NativeTemplate(Template):
"""
Template that renders to native Python types.
Automatically uses NativeEnvironment as its environment class.
"""
def render(self, *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.
Parameters:
*args: Template context arguments
**kwargs: Template context keyword arguments
Returns:
Any: Native Python type or string
"""
def render_async(self, *args, **kwargs):
"""
Async version of render() for native type rendering.
Parameters:
*args: Template context arguments
**kwargs: Template context keyword arguments
Returns:
Any: Native Python type or string
"""Core function that handles conversion from template output to native Python types.
def native_concat(values):
"""
Return a native Python type from the list of compiled nodes.
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.
Parameters:
values: Iterable of outputs to concatenate
Returns:
Any: Native Python type or None
"""from jinja2.nativetypes import NativeEnvironment, NativeTemplate
# Using NativeEnvironment
env = NativeEnvironment()
# Template returning integer
template = env.from_string('{{ 42 }}')
result = template.render()
assert result == 42 # Integer, not string "42"
# Template returning list
template = env.from_string('{{ items }}')
result = template.render(items=[1, 2, 3])
assert result == [1, 2, 3] # List, not string "[1, 2, 3]"
# Template returning dictionary
template = env.from_string('{{ {"name": name, "age": age} }}')
result = template.render(name="Alice", age=30)
assert result == {"name": "Alice", "age": 30} # Dict, not stringfrom jinja2.nativetypes import NativeTemplate
# Create template directly
template = NativeTemplate('{{ value * 2 }}')
result = template.render(value=21)
assert result == 42 # Integer 42, not string "42"
# Template with complex expression
template = NativeTemplate('{{ [x for x in range(count)] }}')
result = template.render(count=5)
assert result == [0, 1, 2, 3, 4] # List of integersfrom jinja2.nativetypes import NativeEnvironment
env = NativeEnvironment()
# Mixed content falls back to string
template = env.from_string('The answer is {{ 42 }}')
result = template.render()
assert result == "The answer is 42" # String fallback
# Pure value returns native type
template = env.from_string('{{ 42 }}')
result = template.render()
assert result == 42 # Native integerimport asyncio
from jinja2.nativetypes import NativeEnvironment
async def main():
env = NativeEnvironment(enable_async=True)
template = env.from_string('{{ await_value }}')
result = await template.render_async(await_value=42)
assert result == 42
asyncio.run(main())class NativeEnvironment(Environment):
"""
Environment that renders templates to native Python types.
Inherits all Environment functionality but uses specialized code generation
and concatenation to produce native Python values when possible.
"""
class NativeTemplate(Template):
"""
Template that renders to native Python types.
Attributes:
environment_class: Set to NativeEnvironment automatically
"""