Flask Rendering Templates render_template Usage


Flask lets you use templates for dynamic web page content. An example project structure for using templates is as follows:



from flask import Flask, render_template

app = Flask(__name__)

def index():
    pagetitle = "HomePage"
    return render_template("index.html",
                            mycontent="Hello World")

Note that you can pass dynamic content from your route handler to the template by appending key/value pairs to the render_templates function. In the above example, the "pagetitle" and "mycontent" variables will be passed to the template for inclusion in the rendered page. Include these variables in the template by enclosing them in double braces: {{mytitle}}


        <title>{{ mytitle }}</title>
        <p>{{ mycontent }}</p>

When executed same as the first example, http://localhost:5000/ will have the title "HomePage" and a paragraph with the content "Hello World".