wip
This commit is contained in:
parent
1df0dbe178
commit
59e3a7d2c8
32
lib.go
32
lib.go
@ -164,6 +164,10 @@ func ScriptContent(t Tag) Tag {
|
|||||||
return t()(renderGuide{script: true})
|
return t()(renderGuide{script: true})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InlineChildren(t Tag) Tag {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
// inline tags are not broken into separate lines when rendering with indentation
|
// inline tags are not broken into separate lines when rendering with indentation
|
||||||
// deprecated in HTML, but only used for indentation
|
// deprecated in HTML, but only used for indentation
|
||||||
func Inline(t Tag) Tag {
|
func Inline(t Tag) Tag {
|
||||||
@ -210,11 +214,31 @@ func FromTemplate[Data any](f Template[Data]) Tag {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// in the functional programming sense
|
// in the functional programming sense
|
||||||
func Map(data []any, tag Tag) []Tag {
|
func Map[Data any](data []Data, tag Tag, tags ...Tag) []Tag {
|
||||||
var tags []Tag
|
var ret []Tag
|
||||||
for _, d := range data {
|
for _, d := range data {
|
||||||
tags = append(tags, tag(d))
|
var retd Tag
|
||||||
|
for i := len(tags) - 1; i >= 0; i-- {
|
||||||
|
retd = tags[i](d)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tags
|
if retd == nil {
|
||||||
|
retd = tag(d)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
retd = tag(retd)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func MapChildren[Data any](data []Data, tag Tag, tags ...Tag) []any {
|
||||||
|
var a []any
|
||||||
|
c := Map(data, tag, tags...)
|
||||||
|
for _, ci := range c {
|
||||||
|
a = append(a, ci)
|
||||||
|
}
|
||||||
|
|
||||||
|
return a
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"code.squareroundforest.org/arpio/html"
|
"code.squareroundforest.org/arpio/html"
|
||||||
. "code.squareroundforest.org/arpio/html/tags"
|
. "code.squareroundforest.org/arpio/html/tags"
|
||||||
"testing"
|
"testing"
|
||||||
|
"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLib(t *testing.T) {
|
func TestLib(t *testing.T) {
|
||||||
@ -22,7 +23,7 @@ func TestLib(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
memberHTML := html.FromTemplate(
|
memberHTML := html.FromTemplate(
|
||||||
func(m member) Tag {
|
func(m member) html.Tag {
|
||||||
return Li(
|
return Li(
|
||||||
Div("Name: ", m.name),
|
Div("Name: ", m.name),
|
||||||
Div("Level: ", m.level),
|
Div("Level: ", m.level),
|
||||||
@ -31,11 +32,11 @@ func TestLib(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
teamHTML := html.FromTemplate(
|
teamHTML := html.FromTemplate(
|
||||||
func(t team) Tag {
|
func(t team) html.Tag {
|
||||||
return Div(
|
return Div(
|
||||||
H3(t.name),
|
H3(t.name),
|
||||||
P("Rank: ", t.rank),
|
P("Rank: ", t.rank),
|
||||||
Ul(html.Map(t.members, memberHTML)...),
|
Ul(html.MapChildren(t.members, memberHTML)...),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -56,7 +57,7 @@ func TestLib(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
if html.RenderIndent(&b, "\t", 0, teamHTML(myTeam)); err != nil {
|
if err := html.RenderIndent(&b, "\t", 0, teamHTML(myTeam)); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,3 +3,8 @@ like <br>: inline void
|
|||||||
inline <hr>: block void
|
inline <hr>: block void
|
||||||
like script: no escaping
|
like script: no escaping
|
||||||
split the validation from the rendering
|
split the validation from the rendering
|
||||||
|
take copies of the children to ensure immutability
|
||||||
|
explain the immutability guarantee in the Go docs: for children yes, for children references no. The general
|
||||||
|
recommendation is not to mutate children. Ofc, creatively breaking the rules is always well appreciated by the
|
||||||
|
right audience
|
||||||
|
test wrapped templates
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user