Skip to content

Commit 753e7b7

Browse files
committed
Added a new attrs option to render_budnle tag and documented webpack_static tag
1 parent 9eae37f commit 753e7b7

File tree

6 files changed

+28
-12
lines changed

6 files changed

+28
-12
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ WEBPACK_LOADER = {
235235
{% render_bundle 'main' 'css' 'DASHBOARD' %}
236236
{% render_bundle 'main' extension='css' config='DASHBOARD' %}
237237
{% render_bundle 'main' config='DASHBOARD' extension='css' %}
238+
239+
<!-- add some extra attributes to the tag -->
240+
{% render_bundle 'main' 'js' 'DEFAULT' attrs='async chatset="UTF-8"'%}
238241
</body>
239242
</head>
240243
```
@@ -260,6 +263,18 @@ CKEDITOR.config.contentsCss = '{{ editor_css_files.0.publicPath }}';
260263
</ul>
261264
```
262265
266+
#### Refer other static assets
267+
268+
`webpack_static` template tag provides facilities to load static assets managed by webpack
269+
in django templates. It is like django's built in `static` tag but for webpack assets instead.
270+
In the below example, `logo.png` can be any static asset shipped with any npm or bower package.
271+
272+
```HTML+Django
273+
{% load webpack_static from webpack_loader %}
274+
275+
<!-- render full public path of logo.png -->
276+
<img src="{% webpack_static 'logo.png' %}"/>
277+
```
263278
264279
<br>
265280

examples/simple/db.sqlite3

33 KB
Binary file not shown.

examples/simple/webpack-stats.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"status":"done","chunks":{"main":[{"name":"main-ffc7dc21b0f5761ce41a.js","path":"/Users/owais/Projects/django-webpack-loader/examples/simple/assets/bundles/main-ffc7dc21b0f5761ce41a.js"}]}}

tests/app/templates/home.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<img src="{% webpack_static 'my-image.png' %}"/>
1313
<img src="{% webpack_static 'my-image.png' 'APP2'%}"/>
1414
<div id="react-app"></div>
15-
{% render_bundle 'main' 'js' %}
15+
{% render_bundle 'main' 'js' attrs='async charset="UTF-8"'%}
1616
{% render_bundle 'app2' 'js' config='APP2' %}
1717
</body>
1818
</html>

tests/app/tests/test_webpack.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ def test_templatetags(self):
9393
view = TemplateView.as_view(template_name='home.html')
9494
request = self.factory.get('/')
9595
result = view(request)
96-
self.assertIn('<link type="text/css" href="/static/bundles/styles.css" rel="stylesheet"/>', result.rendered_content)
97-
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js"></script>', result.rendered_content)
96+
self.assertIn('<link type="text/css" href="/static/bundles/styles.css" rel="stylesheet" />', result.rendered_content)
97+
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" async charset="UTF-8"></script>', result.rendered_content)
9898

99-
self.assertIn('<link type="text/css" href="/static/bundles/styles-app2.css" rel="stylesheet"/>', result.rendered_content)
100-
self.assertIn('<script type="text/javascript" src="/static/bundles/app2.js"></script>', result.rendered_content)
99+
self.assertIn('<link type="text/css" href="/static/bundles/styles-app2.css" rel="stylesheet" />', result.rendered_content)
100+
self.assertIn('<script type="text/javascript" src="/static/bundles/app2.js" ></script>', result.rendered_content)
101101
self.assertIn('<img src="/static/my-image.png"/>', result.rendered_content)
102102

103103
view = TemplateView.as_view(template_name='only_files.html')

webpack_loader/templatetags/webpack_loader.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ def filter_by_extension(bundle, extension):
1414
yield chunk
1515

1616

17-
def render_as_tags(bundle):
17+
def render_as_tags(bundle, attrs):
1818
tags = []
1919
for chunk in bundle:
2020
if chunk['name'].endswith('.js'):
2121
tags.append((
22-
'<script type="text/javascript" src="{0}"></script>'
23-
).format(chunk['url']))
22+
'<script type="text/javascript" src="{0}" {1}></script>'
23+
).format(chunk['url'], attrs))
2424
elif chunk['name'].endswith('.css'):
2525
tags.append((
26-
'<link type="text/css" href="{0}" rel="stylesheet"/>'
27-
).format(chunk['url']))
26+
'<link type="text/css" href="{0}" rel="stylesheet" {1}/>'
27+
).format(chunk['url'], attrs))
2828
return mark_safe('\n'.join(tags))
2929

3030

@@ -36,8 +36,8 @@ def _get_bundle(bundle_name, extension, config):
3636

3737

3838
@register.simple_tag
39-
def render_bundle(bundle_name, extension=None, config='DEFAULT'):
40-
return render_as_tags(_get_bundle(bundle_name, extension, config))
39+
def render_bundle(bundle_name, extension=None, config='DEFAULT', attrs=''):
40+
return render_as_tags(_get_bundle(bundle_name, extension, config), attrs)
4141

4242

4343
@register.simple_tag

0 commit comments

Comments
 (0)