File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package veun_test
2+
3+ import (
4+ "html/template"
5+ "testing"
6+
7+ "github.com/alecthomas/assert/v2"
8+
9+ . "github.com/stanistan/veun"
10+ )
11+
12+ func TestRenderContainerAsView (t * testing.T ) {
13+ html , err := Render (View {
14+ Tpl : containerViewTpl ,
15+ Slots : map [string ]Renderable {
16+ "heading" : ChildView1 {},
17+ "body" : ChildView2 {},
18+ },
19+ })
20+ assert .NoError (t , err )
21+ assert .Equal (t , template .HTML (`<div>
22+ <div class="heading">HEADING</div>
23+ <div class="body">BODY</div>
24+ </div>` ), html )
25+
26+ }
Original file line number Diff line number Diff line change @@ -9,24 +9,12 @@ import (
99 . "github.com/stanistan/veun"
1010)
1111
12- func slotFuncStub (name string ) (template.HTML , error ) {
13- return template .HTML ("" ), nil
14- }
15-
1612type ContainerView struct {
1713 Heading Renderable
1814 Body Renderable
1915}
2016
21- func mustParseTemplate (name , contents string ) * template.Template {
22- return template .Must (
23- template .New (name ).
24- Funcs (template.FuncMap {"slot" : slotFuncStub }).
25- Parse (contents ),
26- )
27- }
28-
29- var containerViewTpl = mustParseTemplate ("containerView" , `<div>
17+ var containerViewTpl = MustParseTemplate ("containerView" , `<div>
3018 <div class="heading">{{ slot "heading" }}</div>
3119 <div class="body">{{ slot "body" }}</div>
3220</div>` )
Original file line number Diff line number Diff line change 1+ package veun
2+
3+ import "html/template"
4+
5+ type View struct {
6+ Tpl * template.Template
7+ Slots map [string ]Renderable
8+ Data any
9+ }
10+
11+ func (v View ) Template () (* template.Template , error ) {
12+ return tplWithRealSlotFunc (v .Tpl , v .Slots ), nil
13+ }
14+
15+ func (v View ) TemplateData () (any , error ) {
16+ return v .Data , nil
17+ }
18+
19+ func tplWithRealSlotFunc (tpl * template.Template , slots map [string ]Renderable ) * template.Template {
20+ return tpl .Funcs (template.FuncMap {
21+ "slot" : func (name string ) (template.HTML , error ) {
22+ slot , ok := slots [name ]
23+ if ok {
24+ return Render (slot )
25+ }
26+ return template .HTML ("" ), nil
27+ },
28+ })
29+ }
30+
31+ func slotFuncStub (name string ) (template.HTML , error ) {
32+ return template .HTML ("" ), nil
33+ }
34+
35+ func MustParseTemplate (name , contents string ) * template.Template {
36+ return template .Must (
37+ template .New (name ).
38+ Funcs (template.FuncMap {"slot" : slotFuncStub }).
39+ Parse (contents ),
40+ )
41+ }
You can’t perform that action at this time.
0 commit comments