-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add spread attribute feature (#237)
Co-authored-by: Joe Davidson <[email protected]> Co-authored-by: Adrian Hesketh <[email protected]>
- Loading branch information
1 parent
83497bc
commit 37f022a
Showing
12 changed files
with
476 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div> | ||
<a bool dateid="my-custom-id" hx-get="/page" id="test" nonshade optional-from-func-true text="lorem"> | ||
text | ||
</a> | ||
<div bool dateid="my-custom-id" hx-get="/page" id="test" nonshade optional-from-func-true text="lorem"> | ||
text2 | ||
</div> | ||
<div> | ||
text3 | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package testspreadattributes | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/a-h/templ" | ||
"github.com/a-h/templ/generator/htmldiff" | ||
) | ||
|
||
//go:embed expected.html | ||
var expected string | ||
|
||
func Test(t *testing.T) { | ||
component := BasicTemplate(templ.Attributes{ | ||
// Should render as `bool` as the value is true, and the conditional render is also true. | ||
"bool": templ.KV(true, true), | ||
// Should not render, as the conditional render value is false. | ||
"bool-disabled": templ.KV(true, false), | ||
// Should render as `dateId="my-custom-id"`. | ||
"dateId": "my-custom-id", | ||
// Should render as `hx-get="/page"`. | ||
"hx-get": "/page", | ||
// Should render as `id="test"`. | ||
"id": "test", | ||
// Should not render, as the attribute value, and the conditional render value is false. | ||
"no-bool": templ.KV(false, false), | ||
// Should not render, as the conditional render value is false. | ||
"no-text": templ.KV("empty", false), | ||
// Should render as `nonshare`, as the value is true. | ||
"nonshade": true, | ||
// Should not render, as the value is false. | ||
"shade": false, | ||
// Should render text="lorem" as the value is true. | ||
"text": templ.KV("lorem", true), | ||
// Optional attribute based on result of func() bool. | ||
"optional-from-func-false": func() bool { return false }, | ||
// Optional attribute based on result of func() bool. | ||
"optional-from-func-true": func() bool { return true }, | ||
}) | ||
|
||
diff, err := htmldiff.Diff(component, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if diff != "" { | ||
t.Error(diff) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package testspreadattributes | ||
|
||
templ BasicTemplate(spread templ.Attributes) { | ||
<div> | ||
<a { spread... }>text</a> | ||
<div | ||
if true { | ||
{ spread... } | ||
} | ||
>text2</div> | ||
<div | ||
if false { | ||
{ spread... } | ||
} | ||
>text3</div> | ||
</div> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.