from jinja2 import Environment tmpl = Environment().from_string( """\ if condition: {{ 1 if foo else 0 }} """ ) print(tmpl.render(foo=True))