|
| 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). |
0 commit comments