Jinja is one of powerful template engine in python.
Template Engine is exists for rendering certain formats(e.g. HTML, XML) with data. So you can reuse it once you make a template. Also, It is used for separating view from logic. You don't have to change view(template) although you change the logic.
I think, that's the reason it is not allowed to write pure python code in jinja.
Variables, Conditional Inclusion like if-then clause, Looping are allowed.
You can find out it here -> Link
But you can call functions to get output that you want from jinja.
from jinja2 import Template
template = "Example Function Call
Function1 {{custom_function1(name)}}
Function2 {{custom_function2(name)}}"
jinja_html_template = Template(template)
def template_function(func):
jinga_html_template.globals[func.__name__] = func
return func
@template_function
def custom_function1(name):
return name + " custom_function1"
@template_function
def custom_function2(name):
return name + " custom_function2"
fields = {'name': 'Sam'}
print(jinga_html_template.render(**fields))
I hope this answer helps you!!
Ciao