Skip to content

Commit 63a4284

Browse files
author
Simon Baechler
committed
new get_chunks tag
add bundle item url tag new get_chunks tag
1 parent 7e43fe1 commit 63a4284

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ WEBPACK_LOADER = {
226226
</head>
227227
```
228228
229+
#### Exposing an asset URL
230+
231+
If you need the URL to an asset without the HTML tags the `get_chunks`
232+
template tag can be used. A common use case is specifying the URL to a
233+
custom css file for a Javascript plugin.
234+
235+
The first parameter is the name of the bundle and is required.
236+
Optional second and third parameters are the extension (css/js)
237+
and the configuration.
238+
239+
```HTML+Django
240+
{% get_chunks 'editor' 'css' as editor_css_chunks %}
241+
CKEDITOR.config.contentsCss = '{{ editor_css_chunks.0.publicPath }}';
242+
```
243+
244+
229245
<br>
230246
231247
## How to use in Production
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% load get_chunks from webpack_loader %}
2+
<script>
3+
4+
{% get_chunks 'main' 'css' as main_css_chunks %}
5+
{% get_chunks 'main' 'js' as main_js_chunks %}
6+
7+
var contentCss = '{{ main_css_chunks.0.url }}';
8+
9+
var contentJS = '{{ main_js_chunks.0.url }}';
10+
11+
</script>

tests/app/tests/test_webpack.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ def test_templatetags(self):
102102
self.assertIn('<script type="text/javascript" src="/static/bundles/app2.js"></script>', result.rendered_content)
103103
self.assertIn('<img src="/static/my-image.png"/>', result.rendered_content)
104104

105+
view = TemplateView.as_view(template_name='custom_config.html')
106+
result = view(request)
107+
self.assertIn("var contentCss = '/static/bundles/styles.css'", result.rendered_content)
108+
self.assertIn("var contentJS = '/static/bundles/main.js'", result.rendered_content)
105109

106110
self.compile_bundles('webpack.config.publicPath.js')
107111
view = TemplateView.as_view(template_name='home.html')

webpack_loader/templatetags/webpack_loader.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ def render_as_tags(bundle):
2424
return '\n'.join(tags)
2525

2626

27-
@register.simple_tag
28-
def render_bundle(bundle_name, extension=None, config='DEFAULT'):
27+
def _get_bundle(bundle_name, extension, config):
2928
bundle = get_bundle(bundle_name, get_config(config))
3029
if extension:
3130
bundle = filter_by_extension(bundle, extension)
32-
return render_as_tags(bundle)
31+
return bundle
32+
33+
34+
@register.simple_tag
35+
def render_bundle(bundle_name, extension=None, config='DEFAULT'):
36+
return render_as_tags(_get_bundle(bundle_name, extension, config))
3337

3438

3539
@register.simple_tag
@@ -38,3 +42,20 @@ def webpack_static(asset_name, config='DEFAULT'):
3842
get_assets(get_config(config)).get('publicPath', getattr(settings, 'STATIC_URL')),
3943
asset_name
4044
)
45+
46+
47+
@register.assignment_tag
48+
def get_chunks(bundle_name, extension=None, config='DEFAULT'):
49+
"""
50+
Returns all chunks in the given bundle.
51+
Example usage::
52+
53+
{% get_chunks 'editor' 'css' as editor_css_chunks %}
54+
CKEDITOR.config.contentsCss = '{{ editor_css_chunks.0.publicPath }}';
55+
56+
:param bundle_name: The name of the bundle
57+
:param extension: (optional) filter by extension
58+
:param config: (optional) the name of the configuration
59+
:return: the whole bundle
60+
"""
61+
return list(_get_bundle(bundle_name, extension, config))

0 commit comments

Comments
 (0)