Skip to content

Commit 57a2c65

Browse files
committed
Renamed get_chunks to get_files.
1 parent 63a4284 commit 57a2c65

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,25 @@ WEBPACK_LOADER = {
226226
</head>
227227
```
228228
229-
#### Exposing an asset URL
229+
#### File URLs instead of html tags
230230
231-
If you need the URL to an asset without the HTML tags the `get_chunks`
231+
If you need the URL to an asset without the HTML tags, the `get_files`
232232
template tag can be used. A common use case is specifying the URL to a
233233
custom css file for a Javascript plugin.
234234
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.
235+
`get_files` works exactly like `render_bundle` except it returns a list of
236+
matching files and lets you assign the list to a custom template variable. For example,
238237
239238
```HTML+Django
240-
{% get_chunks 'editor' 'css' as editor_css_chunks %}
241-
CKEDITOR.config.contentsCss = '{{ editor_css_chunks.0.publicPath }}';
239+
{% get_files 'editor' 'css' as editor_css_files %}
240+
CKEDITOR.config.contentsCss = '{{ editor_css_files.0.publicPath }}';
241+
242+
<!-- or list down name, path and download url for every file -->
243+
<ul>
244+
{% for css_file in editor_css_files %}
245+
<li>{{ css_file.name }} : {{ css_file.path }} : {{ css_file.publicPath }}</li>
246+
{% endfor %}
247+
</ul>
242248
```
243249
244250

tests/app/templates/custom_config.html

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/app/templates/only_files.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% load get_files from webpack_loader %}
2+
<script>
3+
4+
{% get_files 'main' 'css' as main_css_files %}
5+
{% get_files 'main' 'js' as main_js_files %}
6+
7+
var contentCss = '{{ main_css_files.0.url }}';
8+
9+
var contentJS = '{{ main_js_files.0.url }}';
10+
11+
</script>

tests/app/tests/test_webpack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ 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')
105+
view = TemplateView.as_view(template_name='only_files.html')
106106
result = view(request)
107107
self.assertIn("var contentCss = '/static/bundles/styles.css'", result.rendered_content)
108108
self.assertIn("var contentJS = '/static/bundles/main.js'", result.rendered_content)

webpack_loader/templatetags/webpack_loader.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,25 @@ def render_bundle(bundle_name, extension=None, config='DEFAULT'):
3939
@register.simple_tag
4040
def webpack_static(asset_name, config='DEFAULT'):
4141
return "{}{}".format(
42-
get_assets(get_config(config)).get('publicPath', getattr(settings, 'STATIC_URL')),
42+
get_assets(get_config(config)).get(
43+
'publicPath', getattr(settings, 'STATIC_URL')
44+
),
4345
asset_name
4446
)
4547

4648

4749
@register.assignment_tag
48-
def get_chunks(bundle_name, extension=None, config='DEFAULT'):
50+
def get_files(bundle_name, extension=None, config='DEFAULT'):
4951
"""
5052
Returns all chunks in the given bundle.
5153
Example usage::
5254
53-
{% get_chunks 'editor' 'css' as editor_css_chunks %}
55+
{% get_files 'editor' 'css' as editor_css_chunks %}
5456
CKEDITOR.config.contentsCss = '{{ editor_css_chunks.0.publicPath }}';
5557
5658
:param bundle_name: The name of the bundle
5759
:param extension: (optional) filter by extension
5860
:param config: (optional) the name of the configuration
59-
:return: the whole bundle
61+
:return: a list of matching chunks
6062
"""
6163
return list(_get_bundle(bundle_name, extension, config))

0 commit comments

Comments
 (0)