Template Responses#
Panther provides TemplateResponse
for serving HTML templates with dynamic content. This guide explains how to configure and use templates in your Panther application.
Configuration#
To use TemplateResponse
with template files, you must configure the TEMPLATES_DIR
setting in your application configuration.
Setting Templates Directory#
The TEMPLATES_DIR
can be a single string or a list of strings representing template directory paths:
# Single directory
TEMPLATES_DIR = 'templates/'
# Multiple directories (searched in order)
TEMPLATES_DIR = ['templates/', 'app/templates/', 'shared/templates/']
Default value: './'
(current directory)
Usage#
Using Template Files (Recommended)#
When you have template files, use the name
parameter to specify the template file:
1 2 3 4 5 6 7 8 9 10 |
|
Benefits:
- Cleaner code separation
- Template reusability
- Better maintainability
Using Inline HTML Content#
For simple cases or when you need to generate HTML dynamically, you can pass the HTML content directly:
1 2 3 4 5 6 7 8 9 10 11 |
|
Note: This approach requires you to manage the HTML content manually and doesn't provide the benefits of template files.
Template Context#
The context
parameter allows you to pass variables to your templates:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Example Project Structure#
my_panther_app/
├── core/
│ ├── __init__.py
│ ├── configs.py
│ └── urls.py
├── app/
│ ├── __init__.py
│ ├── urls.py
│ └── views.py
├── templates/
│ ├── base.html
│ ├── index.html
│ └── profile.html
└── main.py
With this structure, your configuration would be:
TEMPLATES_DIR = 'templates/'