Skip to content

Commit 834b563

Browse files
destosowais
authored andcommitted
Added support for jinja2 temlates.
* Jinja2 Configuration If you need to output your assets in a jinja template we provide a Jinja2 extension that's compatible with the [Django Jinja](https://github.com/niwinz/django-jinja) module and Django 1.8. To install the extension add it to the django_jinja `TEMPLATES` configuration in the `["OPTIONS"]["extension"]` list. ```python TEMPLATES = [ { "BACKEND": "django_jinja.backend.Jinja2", "OPTIONS": { "extensions": [ "django_jinja.builtins.extensions.DjangoFiltersExtension", "webpack_loader.contrib.jinja2ext.WebpackExtension", ], } } ] ```
1 parent 9e0e46f commit 834b563

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,33 @@ if not DEBUG:
178178
179179
You can also simply generate the bundles on the server before running collectstatic if that works for you.
180180
181+
## Extra
182+
183+
### Jinja2 Configuration
184+
185+
If you need to output your assets in a jinja template we provide a Jinja2 extension that's compatible with the [Django Jinja](https://github.com/niwinz/django-jinja) module and Django 1.8.
186+
187+
To install the extension add it to the django_jinja `TEMPLATES` configuration in the `["OPTIONS"]["extension"]` list.
188+
189+
```python
190+
TEMPLATES = [
191+
{
192+
"BACKEND": "django_jinja.backend.Jinja2",
193+
"OPTIONS": {
194+
"extensions": [
195+
"django_jinja.builtins.extensions.DjangoFiltersExtension",
196+
"webpack_loader.contrib.jinja2ext.WebpackExtension",
197+
],
198+
}
199+
}
200+
]
201+
```
202+
203+
Then in your base jinja template:
204+
205+
```HTML+Jinja2
206+
{{ render_bundle('main') }}
207+
```
181208
182209
--------------------
183210
<br>

webpack_loader/contrib/__init__.py

Whitespace-only changes.

webpack_loader/contrib/jinja2ext.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import jinja2
2+
3+
from ..templatetags.webpack_loader import render_bundle
4+
5+
6+
class WebpackExtension(jinja2.ext.Extension):
7+
def __init__(self, environment):
8+
super(WebpackExtension, self).__init__(environment)
9+
environment.globals["render_bundle"] = lambda name: jinja2.Markup(render_bundle(name))

webpack_loader/templatetags/webpack_loader.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def render_bundle(bundle_name):
1212
bundle = get_bundle(bundle_name)
1313
tags = []
1414
for chunk in bundle:
15-
1615
if chunk['name'].endswith('.js'):
1716
url = chunk.get('publicPath') or chunk['url']
1817
tags.append('<script type="text/javascript" src="{}"></script>'.format(url))

0 commit comments

Comments
 (0)