Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 1f4a172

Browse files
authored
Make Jinja2 support easier (#15)
1 parent d96abbc commit 1f4a172

File tree

11 files changed

+208
-12
lines changed

11 files changed

+208
-12
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,60 @@ When it renders you should get something like this:
138138
</html>
139139
```
140140

141+
## Using Jinja2
142+
143+
To use django-importmap with Jinja2 templates,
144+
you can add `importmap` to a customized Jinja environment.
145+
146+
```python
147+
TEMPLATES = [
148+
{
149+
"BACKEND": "django.template.backends.jinja2.Jinja2",
150+
...
151+
"OPTIONS": {
152+
"environment": "app.jinja2.environment",
153+
...
154+
},
155+
}
156+
]
157+
```
158+
159+
Then in `app/jinja2.py`:
160+
161+
```python
162+
from django.conf import settings
163+
from jinja2 import Environment
164+
165+
from importmap import Importmap
166+
167+
168+
def environment(**options):
169+
env = Environment(**options)
170+
env.globals.update({"importmap": Importmap.json(development=settings.DEBUG)})
171+
return env
172+
```
173+
174+
Then in your Jinja templates you can include a module shim and output the `importmap` variable like this:
175+
176+
```html
177+
<!DOCTYPE html>
178+
<html lang="en">
179+
<head>
180+
<script async src="https://ga.jspm.io/npm:es-module-shims@1.3.6/dist/es-module-shims.js"></script>
181+
<script type="importmap">
182+
{{ importmap|safe }}
183+
</script>
184+
<script type="module">
185+
import React from "react"
186+
console.log(React);
187+
</script>
188+
</head>
189+
<body>
190+
191+
</body>
192+
</html>
193+
```
194+
141195
## Project status
142196

143197
This is partly an experiment,

importmap/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .core import Importmap

importmap/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ def __init__(
4646
self.lock_filename = lock_filename
4747
self.load()
4848

49+
@classmethod
50+
def json(cls, development=False):
51+
importmap = cls()
52+
53+
if development:
54+
return json.dumps(importmap.map_dev, indent=2, sort_keys=True)
55+
else:
56+
return json.dumps(importmap.map, sort_keys=True)
57+
4958
def load(self):
5059
# TODO django check to compare lock and config hash
5160

importmap/templatetags/importmap.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@
1010

1111
@register.inclusion_tag("importmap/scripts.html")
1212
def importmap_scripts():
13-
importmap = Importmap()
14-
15-
if settings.DEBUG:
16-
return {"importmap": json.dumps(importmap.map_dev, indent=2, sort_keys=True)}
17-
else:
18-
return {"importmap": json.dumps(importmap.map, sort_keys=True)}
13+
return {"importmap": Importmap.json(development=settings.DEBUG)}

poetry.lock

Lines changed: 79 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ black = "^22.1.0"
3636
Django = "^4.0.0"
3737
ipdb = "^0.13.9"
3838
isort = "^5.10.1"
39+
jinja2 = "^3.1.2"
3940

4041
[build-system]
4142
requires = ["poetry-core>=1.0.0"]

test_project/app/jinja2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.conf import settings
2+
from jinja2 import Environment
3+
4+
from importmap import Importmap
5+
6+
7+
def environment(**options):
8+
env = Environment(**options)
9+
env.globals.update({"importmap": Importmap.json(development=settings.DEBUG)})
10+
return env

test_project/app/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
33

44
urlpatterns = [
55
path("", TemplateView.as_view(template_name="index.html"), name="index"),
6+
path(
7+
"jinja",
8+
TemplateView.as_view(template_name="index_jinja.html"),
9+
name="index_jinja",
10+
),
611
]

test_project/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@
6767
],
6868
},
6969
},
70+
{
71+
"BACKEND": "django.template.backends.jinja2.Jinja2",
72+
"DIRS": [Path(__file__).resolve().parent / "templates" / "jinja2"],
73+
"APP_DIRS": False,
74+
"OPTIONS": {
75+
"environment": "app.jinja2.environment",
76+
},
77+
},
7078
]
7179

7280
WSGI_APPLICATION = "wsgi.application"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
9+
<script async src="https://ga.jspm.io/npm:es-module-shims@1.3.6/dist/es-module-shims.js"></script>
10+
<script type="importmap">
11+
{{ importmap|safe }}
12+
</script>
13+
</head>
14+
<body>
15+
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)