# Template rendering using Jinja2

Template rendering is useful for building web pages, dashboards, or hybrid applications where HTML is generated on the server.

## Configuring Templates

Before rendering templates, configure the template directory using `configure_templates()`:

```python
from slush.app import Slush

app = Slush()
app.configure_templates("./templates")
```

The provided path should point to the directory containing your template files.

## Basic Usage

Create a template file:

```
templates/index.html
```

Example template:

```html
<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>Hello {{ name }}</h1>
</body>
</html>
```

Render the template in a route:

```python
@app.route("/", methods=["GET"])
def home(request):
    return app.render_template("index.html", {
        "title": "Home Page",
        "name": "Faraz"
    })
```

## Passing Data to Templates

Template variables are passed as a dictionary:

```python
return app.render_template("index.html", {
    "title": "Dashboard",
    "user": "Faraz"
})
```

Inside the template:

```html
<h1>Welcome {{ user }}</h1>
```

## Using Static Files with Templates

Templates can reference static assets using the configured static path:

```html
<link rel="stylesheet" href="/static/style.css">
```

Make sure static files are mounted:

```python
app.mount_static("/static", "./static")
```

## Template Not Found

If the specified template does not exist, Slush returns a `500` response indicating the missing template.

Example:

```python
return app.render_template("missing.html", {})
```

## Rendering HTML Responses

Rendered templates are returned as HTML responses with the correct content type automatically set.

## Example Project Structure

```
project/
├── main.py
├── templates/
│   └── index.html
├── static/
│   └── style.css
```

## When to Use Templates

Use template rendering when:

* Building server-rendered web pages
* Creating dashboards or admin interfaces
* Combining backend logic with HTML output

For API-only applications, returning JSON responses may be sufficient.

## Summary

Template rendering in Slush allows you to:

* Generate dynamic HTML using Jinja2
* Pass data from routes into templates
* Integrate static assets easily
* Build full web interfaces alongside APIs


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://slush.gitbook.io/docs/documentation/basics/template-rendering-using-jinja2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
