Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom kebab-case attributes #131

Merged
merged 3 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading