liveBook · Manning

Jinja

As explained in Coding with AI

Jinja2 is a Python templating engine that renders text or HTML from templates containing expressions and control directives.

Jinja is Flask’s templating system: Python route code passes data into an HTML template, where expressions display values and control directives repeat or organize markup. section 5.3.3 section 7.1.1

Render database results on an index page

A Flask route calls a data function and passes its result to index.html with render_template. The template loops over data and prints each item with {{ item }}. When errors appear, verify imports, the template location, and the application’s expected templates folder; generated suggestions may use incorrect names or file structures. section 5.3.3

# Import the function that contains the data you want to extract
from my_module import get_data
# Create a route for the HTML index page
@app.route('/')
def index():
    # Call the function and store the returned data in a variable
    data = get_data()
    # Return the HTML template for the index page
    return render_template('index.html', data=data)
<!-- HTML template for the index page -->
<!DOCTYPE html>
<html>
<head>
    <title>Index Page</title>
</head>
<body>
    <h1>Data</h1>
    <ul>
        {% for item in data %}
            <li>{{ item }}</li>
        {% endfor %}
    </ul>
</body>
</html>

Shows the complete route-to-template flow, including data retrieval, render_template, a Jinja for-loop, and value interpolation. section 5.3.3

How Coding with AI covers it

How deep it goes
Demonstrates variable interpolation, loops, route integration, and template placement without developing Jinja syntax beyond these rendering tasks.
Examples
Uses database question listings, repeated data items, and a quiz form that displays questions and answer options.
What it assumes
Assumes basic Flask routes, Python functions, HTML templates, and data retrieval while demonstrating their connection from zero in a small workflow.
What you can do after
Build Flask pages that pass Python data into Jinja templates and render values or repeated records in HTML.
How it connects
Links Flask, HTML templates, routes, database queries, and SQLAlchemy-based quiz interfaces.

Also explained in

Compare how 4 books explain Jinja →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Coding with AI. The section links point to the parts of the book it draws on.