Branching in Jinja2
j2_ifthen.py
from flask import * import random app = Flask(__name__) @app.route("/") def main(): if random.randint(1, 2) == 1: return render_template("basic.html", answer="salmon") else: return render_template("basic.html", answer="plum") app.run() #put basic.html in templates folder
templates\basic.html
<!doctype html> <h1>Salmon or Plum?</h1><br> <h1>Python returned: {{answer}} </h1><br> {% if answer == "salmon" %} <h1 style="color:salmon;">Scenic Salmon!</h1> {% else %} <h1 style="color:plum;">Picturesque Plum!</h1> {% endif %}