Skip to content

Commit ab022b9

Browse files
Fix attribute name handling to convert underscores to hyphens (#105)
1 parent 66ed6e1 commit ab022b9

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Fixed
22+
23+
- Fixed attribute name handling in components to properly convert underscores to hyphens (e.g. `hx_get` becomes `hx-get`) for better HTML compatibility.
24+
2125
## [0.9.0]
2226

2327
### Added

docs/params.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ This will render as:
6767
</button>
6868
```
6969

70+
### Attribute Names
71+
72+
When rendering attributes, underscores in attribute names are automatically converted to hyphens. This is particularly useful for data attributes and other hyphenated HTML attributes:
73+
74+
```htmldjango
75+
{% bird button data_analytics="signup" hx_get="/api/data" %}
76+
Load Data
77+
{% endbird %}
78+
```
79+
80+
This will render as:
81+
82+
```html
83+
<button data-analytics="signup" hx-get="/api/data">
84+
Load Data
85+
</button>
86+
```
87+
7088
## Properties
7189

7290
Properties (i.e. `props`) allow you to define parameters that your component expects, with optional default values. Unlike attributes which are provided as a flattened string via `{{ attrs }}`, props are processed by the component and made available as individual values (e.g. `{{ props.variant }}`) that can be used to control rendering logic.

src/django_bird/params.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@ class Param:
3838
name: str
3939
value: Value
4040

41-
def _resolve_value(self, context: Context) -> str | bool | None:
42-
return self.value.resolve(context)
43-
4441
def render_attr(self, context: Context) -> str:
45-
value = self._resolve_value(context)
42+
value = self.value.resolve(context)
43+
name = self.name.replace("_", "-")
4644
if value is None:
4745
return ""
4846
if value is True:
49-
return self.name
50-
return f'{self.name}="{value}"'
47+
return name
48+
return f'{name}="{value}"'
5149

5250
def render_prop(self, context: Context) -> str | bool | None:
53-
return self._resolve_value(context)
51+
return self.value.resolve(context)
5452

5553
@classmethod
5654
def from_bit(cls, bit: str) -> Param:

tests/test_params.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class TestParam:
6464
{"foo": {}},
6565
'class="foo.bar"',
6666
),
67+
(Param(name="hx_get", value=Value("/", quoted=False)), {}, 'hx-get="/"'),
6768
],
6869
)
6970
def test_render_attr(self, param, context, expected):

0 commit comments

Comments
 (0)