Block 62: Flask: Templates & Jinja2
Render dynamic HTML pages using Jinja2 templates.
Concepts
- templates/ folder structure
- render_template('page.html', var=value)
- Jinja2 syntax: {{ var }}, {% if %}, {% for %}
- Template inheritance: {% extends %} and {% block %}
Code Examples
See exercise below.
Exercise
Create a template that displays a list of items passed from Flask. Add a base.html template with a navbar and extend it in 2 other pages.
Homework
How does Jinja2 template inheritance reduce code duplication? Give a concrete example. Tuesday