Skip to content

Commit 32ecc9e

Browse files
author
User
committed
Remove optional jinja2 dependency, add import guard, add Jinja docs page
- Remove [project.optional-dependencies] jinja = ["jinja2"] from pyproject.toml - Add try/except ModuleNotFoundError guard at the top of jinja.py with a helpful error message telling users to install jinja2 - Create docs/src/learn/add-reactpy-to-a-django-project-with-jinja.md with Jinja2-specific setup instructions - Update mkdocs.yml nav to include the new Jinja page under Get Started
1 parent 014fa4b commit 32ecc9e

4 files changed

Lines changed: 145 additions & 3 deletions

File tree

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ nav:
33
- Home: index.md
44
- Get Started:
55
- Add ReactPy to a Django Project: learn/add-reactpy-to-a-django-project.md
6+
- Add ReactPy to a Django Project (with Jinja): learn/add-reactpy-to-a-django-project-with-jinja.md
67
- Your First Component: learn/your-first-component.md
78
- Reference:
89
- Components: reference/components.md
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
## Overview
2+
3+
<p class="intro" markdown>
4+
5+
If you use **Jinja2 templates** in your Django project and want to add ReactPy interactivity, this guide walks you through the additional configuration needed. First complete the [standard setup](./add-reactpy-to-a-django-project.md), then follow the steps below.
6+
7+
</p>
8+
9+
!!! abstract "Note"
10+
11+
These docs assume you have already completed the [standard ReactPy-Django setup](./add-reactpy-to-a-django-project.md) and have a working **Django project** with Jinja2 configured.
12+
13+
---
14+
15+
## Step 1: Install `jinja2`
16+
17+
Jinja2 is a Python dependency you must install in your environment to use this feature.
18+
19+
```bash linenums="0"
20+
pip install jinja2
21+
```
22+
23+
---
24+
25+
## Step 2: Configure the Jinja2 template engine
26+
27+
Add a Jinja2 backend entry to your `TEMPLATES` list in [`settings.py`](https://docs.djangoproject.com/en/stable/topics/settings/).
28+
29+
The backend must include:
30+
31+
- `"BACKEND": "django.template.backends.jinja2.Jinja2"`
32+
- An `"environment"` option pointing to a function that registers the `ReactPyExtension`
33+
34+
=== "settings.py"
35+
36+
```python
37+
TEMPLATES = [
38+
{
39+
"BACKEND": "django.template.backends.django.DjangoTemplates",
40+
"DIRS": [],
41+
"APP_DIRS": True,
42+
"OPTIONS": {
43+
"context_processors": [
44+
"django.template.context_processors.debug",
45+
"django.template.context_processors.request",
46+
"django.contrib.auth.context_processors.auth",
47+
"django.contrib.messages.context_processors.messages",
48+
],
49+
},
50+
},
51+
{
52+
"BACKEND": "django.template.backends.jinja2.Jinja2",
53+
"DIRS": [],
54+
"OPTIONS": {
55+
"environment": "myproject.jinja_env.environment",
56+
"context_processors": [
57+
"django.template.context_processors.debug",
58+
"django.template.context_processors.request",
59+
"django.contrib.auth.context_processors.auth",
60+
"django.contrib.messages.context_processors.messages",
61+
],
62+
},
63+
},
64+
]
65+
```
66+
67+
---
68+
69+
## Step 3: Create a Jinja2 environment module
70+
71+
Create a `jinja_env.py` module that registers the `ReactPyExtension`:
72+
73+
=== "myproject/jinja_env.py"
74+
75+
```python
76+
from jinja2 import Environment
77+
from reactpy_django.templatetags.jinja import ReactPyExtension
78+
79+
80+
def environment(**options):
81+
env = Environment(**options)
82+
env.add_extension(ReactPyExtension)
83+
return env
84+
```
85+
86+
---
87+
88+
## Step 4: Use ReactPy components in Jinja2 templates
89+
90+
With the extension registered, you can call ReactPy functions directly inside any Jinja2 template:
91+
92+
=== "templates/example.html.jinja"
93+
94+
```html
95+
<!DOCTYPE html>
96+
<html>
97+
<head>
98+
<title>ReactPy + Jinja2</title>
99+
</head>
100+
<body>
101+
<h1>Server-side component</h1>
102+
{{ component("my_app.components.hello_world", recipient="World") }}
103+
104+
<h1>Client-side PyScript component</h1>
105+
{{ pyscript_component("my_app/components/my_app.py") }}
106+
107+
{{ pyscript_setup() }}
108+
</body>
109+
</html>
110+
```
111+
112+
!!! info "Template tag vs Jinja2 function syntax"
113+
114+
Unlike Django templates which require `{% load reactpy %}` and `{% component "..." %}`, Jinja2 allows you to call component functions directly using the `{{ component(...) }}` syntax. The registered functions are:
115+
116+
| Function | Description |
117+
|---|---|---|
118+
| `{{ component(dotted_path, *args, **kwargs) }}` | Render a server-side ReactPy component. Equivalent to `{% component %}`. |
119+
| `{{ pyscript_component(*file_paths, initial, root) }}` | Render a client-side PyScript component. Equivalent to `{% pyscript_component %}`. |
120+
| `{{ pyscript_setup(*extra_py, extra_js, config) }}` | Render PyScript setup configuration. Equivalent to `{% pyscript_setup %}`. |
121+
122+
---
123+
124+
## Step 5: Verify your configuration
125+
126+
Run Django's [`check` command](https://docs.djangoproject.com/en/stable/ref/django-admin/#check) to verify everything is set up correctly.
127+
128+
```bash linenums="0"
129+
python manage.py check
130+
```
131+
132+
---
133+
134+
## Next steps
135+
136+
Now you're ready to [create your first component](./your-first-component.md).

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ urls.Changelog = "https://reactive-python.github.io/reactpy-django/latest/about/
5555
urls.Documentation = "https://reactive-python.github.io/reactpy-django/"
5656
urls.Source = "https://github.com/reactive-python/reactpy-django"
5757

58-
[project.optional-dependencies]
59-
jinja = ["jinja2"]
60-
6158
[tool.hatch.version]
6259
path = "src/reactpy_django/__init__.py"
6360

src/reactpy_django/templatetags/jinja.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def environment(**options):
3636

3737
from __future__ import annotations
3838

39+
try:
40+
import jinja2 # noqa: F401
41+
except ModuleNotFoundError as e:
42+
raise ModuleNotFoundError(
43+
"The `jinja2` package is required to use ReactPy-Django's Jinja2 template support. "
44+
"Install it with: pip install jinja2"
45+
) from e
46+
3947
from logging import getLogger
4048
from typing import TYPE_CHECKING
4149

0 commit comments

Comments
 (0)