Skip to content

Commit 923fc97

Browse files
committed
automate kebab-casing for attributes
1 parent fbf9b07 commit 923fc97

File tree

4 files changed

+8
-39
lines changed

4 files changed

+8
-39
lines changed

README.md

+3-21
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ Any kwargs passed to vite_react_refresh will be added to its generated `<script/
196196
By default, all script tags are generated with a `type="module"` and `crossorigin=""` attributes just like ViteJS do by default if you are building a single-page app.
197197
You can override this behavior by adding or overriding this attributes like so :
198198

199-
```
200-
{% vite_asset '<path to your asset>' foo="bar" hello="world" %}
199+
```jinja-html
200+
{% vite_asset '<path to your asset>' foo="bar" hello="world" data_turbo_track="reload" %}
201201
```
202202

203-
This line will add `foo="bar"` and `hello="world"` attributes.
203+
This line will add `foo="bar"`, `hello="world"`, and `data-turbo-track="reload"` attributes.
204204

205205
You can also use context variables to fill attributes values :
206206

@@ -216,24 +216,6 @@ If you want to overrides default attributes just add them like new attributes :
216216

217217
Although it's recommended to keep the default `type="module"` attribute as ViteJS build scripts as ES6 modules.
218218

219-
You can also set attributes that require `kebab-casing` by padding `json_encoded_attributes`.
220-
221-
```python
222-
from django.utils.safestring import SafeString
223-
224-
def your_view(request):
225-
json_encoded_attributes = json.dumps({
226-
"some-item": "3",
227-
"some-other-item": "value",
228-
})
229-
230-
render(request, template, context={ "json_encoded_attributes": SafeString(json_encoded_attributes) })
231-
```
232-
233-
```html
234-
{% vite_asset '<path to your asset>' json_encoded_attributes=json_encoded_attributes %}
235-
```
236-
237219
## Vite Legacy Plugin
238220

239221
If you want to consider legacy browsers that don't support ES6 modules loading

django_vite/core/asset_loader.py

-5
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,6 @@ def generate_vite_asset(
301301
str -- The <script> tag and all <link> tags to import
302302
this asset in your page.
303303
"""
304-
305-
json_encoded_attributes = kwargs.pop("json_encoded_attributes", "{}")
306-
json_encoded_attributes = json.loads(json_encoded_attributes)
307-
kwargs.update(json_encoded_attributes)
308-
309304
if self.dev_mode:
310305
url = self._get_dev_server_url(path)
311306
return TagGenerator.script(

django_vite/core/tag_generator.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def attrs_to_str(attrs: Dict[str, str]):
88
Convert dictionary of attributes into a string that can be injected into a <script/>
99
tag.
1010
"""
11-
attrs_str = " ".join([f'{key}="{value}"' for key, value in attrs.items()])
11+
attrs_str = " ".join(
12+
[f'{key.replace("_", "-")}="{value}"' for key, value in attrs.items()]
13+
)
1214
return attrs_str
1315

1416

tests/tests/templatetags/test_vite_asset.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import pytest
2-
from django.utils.safestring import SafeString
3-
import json
42
from bs4 import BeautifulSoup
53
from django.template import Context, Template, TemplateSyntaxError
64
from django_vite.core.exceptions import (
@@ -212,21 +210,13 @@ def test_vite_asset_override_default_attribute():
212210

213211
@pytest.mark.usefixtures("dev_mode_all")
214212
def test_vite_asset_kebab_attribute():
215-
additional_attributes = {
216-
"data-item-track": "reload",
217-
"data-other": "3",
218-
}
219213
template = Template(
220214
"""
221215
{% load django_vite %}
222-
{% vite_asset "src/entry.ts" json_encoded_attributes=json_encoded_attributes %}
216+
{% vite_asset "src/entry.ts" data_item_track="reload" data_other="3" %}
223217
"""
224218
)
225-
html = template.render(
226-
Context(
227-
{"json_encoded_attributes": SafeString(json.dumps(additional_attributes))}
228-
)
229-
)
219+
html = template.render(Context({}))
230220
soup = BeautifulSoup(html, "html.parser")
231221
script_tag = soup.find("script")
232222
assert script_tag["data-item-track"] == "reload"

0 commit comments

Comments
 (0)