Flask Rendering Templates (original) (raw)

Flask provides template rendering using the Jinja2 templating engine, which allows HTML pages to display dynamic data. Templates help separate the application logic from the user interface, making web applications easier to manage and build.

Implementing Template Rendering

Step 1: Create a Flask App

First, create a file named app.py and initialize a basic Flask application.

Python `

from flask import Flask app = Flask(name)

if name == "main": app.run()

`

**Explanation:

Step 2: Create the Templates Folder

Flask automatically looks for HTML files inside a folder named templates. Create a templates folder in the project directory and add an index.html file inside it.

**index.html

Python `

Flask App

Welcome to Flask

This is a basic template rendering example.

`

**Explanation:

Step 3: Render HTML Template

Now, update app.py to render the index.html template.

Python `

from flask import Flask, render_template app = Flask(name)

@app.route("/") def index(): return render_template("index.html")

if name == "main": app.run()

`

**Explanation:

Step 4: Pass Dynamic Data Using Jinja2

Flask uses Jinja2 templating to pass dynamic data from Python code to HTML templates. Update app.py with a new route:

Python `

from flask import Flask, render_template app = Flask(name)

@app.route("/") def welcome(name): return render_template("welcome.html", name=name)

if name == "main": app.run()

`

**Explanation:

Step 5: Create a Dynamic HTML Template

Create a file named welcome.html inside the templates folder.

HTML `

FlaskTest

Welcome To GFG

Welcome, {{name}}

`

**Output

jinja-template

Using Jinja template

**Explanation:

Inducing Logic in Templates

Jinja2 templates support logical operations such as loops and conditional statements. This allows HTML pages to display dynamic content based on data passed from Flask routes.

Step 1: Using for Loop in Templates

A for loop in Jinja2 is used to display multiple items dynamically inside an HTML template. Add the following route in app.py:

Python `

@app.route("/about") def about(): sites = ['twitter', 'facebook', 'instagram', 'whatsapp'] return render_template("about.html", sites=sites)

`

**Explanation:

Create about.html inside the templates folder.

HTML `

{% extends 'index.html' %} {% block body %}

{% endblock %}

`

**Explanation:

Navigation links help move between Flask routes easily using url_for(). Update index.html:

HTML `

FlaskTest

Welcome To GFG

Flask: Rendering Templates

Home Index About {% block body %}
<p>This is a Flask application.</p>
{% endblock %}

`

**Explanation:

for-loops-in-templates

Demonstrating for loop-in templates

Step 3: Using if-else Conditions in Templates

Conditional statements in Jinja2 allow templates to display different content based on values received from Flask routes. Add the following route in app.py:

Python `

@app.route("/contact/") def contact(role): return render_template("contact.html", person=role)

`

**Explanation:

Create contact.html inside the templates folder.

HTML `

{% extends 'index.html' %} {% block body %}

{% if person == "admin" %}

Admin Section

{% elif person == "maintainer" %}

App Source Page for Maintainer

{% elif person == "member" %}

Hope you are enjoying our services

{% else %}

Hello, {{ person }}

{% endif %} {% endblock %}

`

In the template, we check the **person variable, which comes from the URL and is passed via **render_template(). The **if-else syntax is similar to Python but enclosed in ****{% %}**. It follows a simple if-elif-else structure to generate HTML based on the value.

**Output

if-in-flask

If Statement Demonstration in Flask

**Explanation: