/
webgo_nested.go
60 lines (49 loc) · 1.21 KB
/
webgo_nested.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"github.com/ziutek/kasia.go"
"github.com/hoisie/web.go"
)
type Ctx struct {
Cnt int
Txt string
Subtpl *kasia.Template // Nested template
Subctx *SubCtx // Subcontext
}
type SubCtx struct {
Vals []string
}
var (
tpl *kasia.Template
data = &Ctx{0, "Hello!", nil, &SubCtx{}}
)
func hello(web_ctx *web.Context, val string) {
// Note that it's not safe to modify global data in multithread enviroment
data.Cnt++
data.Subctx.Vals = append(data.Subctx.Vals, val)
// Render data
err := tpl.Run(web_ctx, data)
if err != nil {
fmt.Fprint(web_ctx, err)
}
}
func main() {
// Main template
tpl = kasia.New()
err := tpl.Parse([]byte("<html><body>Request #$Cnt: $Txt<br>\n" +
"$Subtpl.Nested(Subctx)</body></html>\n"))
if err != nil {
fmt.Println("Main template", err)
return
}
// Nested template
data.Subtpl = kasia.New()
err = data.Subtpl.Parse([]byte("$for i+, v in Vals: $i: $v<br>\n$end"))
if err != nil {
fmt.Println("Nested template", err)
return
}
// Web.go
web.Get("/(.*)", hello)
web.Run("0.0.0.0:9999")
}