I have an if statement in my Jinja templates which I want to write it in multines for readability reasons. Consider the case

{% if (foo == 'foo' or bar == 'bar') and (fooo == 'fooo' or baar == 'baar') etc.. %} 

1 Answer

According to the documentation: you may use multi-line statements as long as the code has parens/brackets around it. Example:

{% if ( (foo == 'foo' or bar == 'bar') and (fooo == 'fooo' or baar == 'baar') ) %} <li>some text</li> {% endif %} 

Edit: Using line_statement_prefix = '#'* the code would look like this:

# if ( (foo == 'foo' or bar == 'bar') and (fooo == 'fooo' or baar == 'baar') ) <li>some text</li> # endif 

*Here's an example of how you'd specify the line_statement_prefix in the Environment:

from jinja2 import Environment, PackageLoader, select_autoescape env = Environment( loader=PackageLoader('yourapplication', 'templates'), autoescape=select_autoescape(['html', 'xml']), line_statement_prefix='#' ) 

Or using Flask:

from flask import Flask app = Flask(__name__, instance_relative_config=True, static_folder='static') app.jinja_env.filters['zip'] = zip app.jinja_env.line_statement_prefix = '#' 
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy