Changed: DB Params
This commit is contained in:
11
templ/generator/test-spread-attributes/expected.html
Normal file
11
templ/generator/test-spread-attributes/expected.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<div>
|
||||
<a bool data-attr="value" data-attr-bool dateid="my-custom-id" hx-get="/page" id="test" nonshade optional-from-func-true text="lorem">
|
||||
text
|
||||
</a>
|
||||
<div bool data-attr="value" data-attr-bool dateid="my-custom-id" hx-get="/page" id="test" nonshade optional-from-func-true text="lorem">
|
||||
text2
|
||||
</div>
|
||||
<div>
|
||||
text3
|
||||
</div>
|
||||
</div>
|
65
templ/generator/test-spread-attributes/render_test.go
Normal file
65
templ/generator/test-spread-attributes/render_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
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 non-nil string values.
|
||||
"data-attr": ptr("value"),
|
||||
// Should render non-nil boolean values that evaluate to true.
|
||||
"data-attr-bool": ptr(true),
|
||||
// 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 a nil string pointer.
|
||||
"key": nilPtr[string](),
|
||||
// Should not render a nil boolean value.
|
||||
"boolkey": nilPtr[bool](),
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
func nilPtr[T any]() *T {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ptr[T any](x T) *T {
|
||||
return &x
|
||||
}
|
17
templ/generator/test-spread-attributes/template.templ
Normal file
17
templ/generator/test-spread-attributes/template.templ
Normal file
@@ -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>
|
||||
}
|
68
templ/generator/test-spread-attributes/template_templ.go
Normal file
68
templ/generator/test-spread-attributes/template_templ.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.3.833
|
||||
package testspreadattributes
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
func BasicTemplate(spread templ.Attributes) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div><a")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templ.RenderAttributes(ctx, templ_7745c5c3_Buffer, spread)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, ">text</a><div")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if true {
|
||||
templ_7745c5c3_Err = templ.RenderAttributes(ctx, templ_7745c5c3_Buffer, spread)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, ">text2</div><div")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if false {
|
||||
templ_7745c5c3_Err = templ.RenderAttributes(ctx, templ_7745c5c3_Buffer, spread)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, ">text3</div></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
Reference in New Issue
Block a user