Skip to content

Commit

Permalink
Merge pull request #131 from Niicck/kebab-case
Browse files Browse the repository at this point in the history
Enable custom kebab-case attributes
  • Loading branch information
Niicck authored Apr 7, 2024
2 parents fee2efe + 923fc97 commit 8690c1e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ Any kwargs passed to vite_react_refresh will be added to its generated `<script/
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.
You can override this behavior by adding or overriding this attributes like so :

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

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

You can also use context variables to fill attributes values :

Expand Down
1 change: 0 additions & 1 deletion django_vite/core/asset_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ def generate_vite_asset(
str -- The <script> tag and all <link> tags to import
this asset in your page.
"""

if self.dev_mode:
url = self._get_dev_server_url(path)
return TagGenerator.script(
Expand Down
4 changes: 3 additions & 1 deletion django_vite/core/tag_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def attrs_to_str(attrs: Dict[str, str]):
Convert dictionary of attributes into a string that can be injected into a <script/>
tag.
"""
attrs_str = " ".join([f'{key}="{value}"' for key, value in attrs.items()])
attrs_str = " ".join(
[f'{key.replace("_", "-")}="{value}"' for key, value in attrs.items()]
)
return attrs_str


Expand Down
15 changes: 15 additions & 0 deletions tests/tests/templatetags/test_vite_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ def test_vite_asset_override_default_attribute():
assert script_tag["crossorigin"] == "anonymous"


@pytest.mark.usefixtures("dev_mode_all")
def test_vite_asset_kebab_attribute():
template = Template(
"""
{% load django_vite %}
{% vite_asset "src/entry.ts" data_item_track="reload" data_other="3" %}
"""
)
html = template.render(Context({}))
soup = BeautifulSoup(html, "html.parser")
script_tag = soup.find("script")
assert script_tag["data-item-track"] == "reload"
assert script_tag["data-other"] == "3"


def test_vite_asset_custom_attributes(dev_mode_all):
template = Template(
"""
Expand Down

0 comments on commit 8690c1e

Please sign in to comment.