tpl: Add docshelper for template funcs

And fix some other minor related issues.

Updates #3418
This commit is contained in:
Bjørn Erik Pedersen
2017-05-01 18:40:34 +02:00
parent e2b067f050
commit 690b0f8ff5
25 changed files with 2064 additions and 347 deletions

View File

@@ -24,48 +24,122 @@ func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ctx := New(d)
examples := [][2]string{
{`{{ delimit (slice "A" "B" "C") ", " " and " }}`, `A, B and C`},
{`{{ echoParam .Params "langCode" }}`, `en`},
{`{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}`, `Substring found!`},
{
`{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
`bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`},
{
`<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
`<a href="https://www.google.com?page=3&amp;q=test">Search</a>`},
{`{{ slice "B" "C" "A" | sort }}`, `[A B C]`},
{`{{ seq 3 }}`, `[1 2 3]`},
{`{{ union (slice 1 2 3) (slice 3 4 5) }}`, `[1 2 3 4 5]`},
}
return &internal.TemplateFuncsNamespace{
ns := &internal.TemplateFuncsNamespace{
Name: name,
Context: func() interface{} { return ctx },
Aliases: map[string]interface{}{
"after": ctx.After,
"apply": ctx.Apply,
"delimit": ctx.Delimit,
"dict": ctx.Dictionary,
"echoParam": ctx.EchoParam,
"first": ctx.First,
"in": ctx.In,
"index": ctx.Index,
"intersect": ctx.Intersect,
"isSet": ctx.IsSet,
"isset": ctx.IsSet,
"last": ctx.Last,
"querify": ctx.Querify,
"shuffle": ctx.Shuffle,
"slice": ctx.Slice,
"sort": ctx.Sort,
"union": ctx.Union,
"where": ctx.Where,
"seq": ctx.Seq,
},
Examples: examples,
}
ns.AddMethodMapping(ctx.After,
[]string{"after"},
[][2]string{},
)
ns.AddMethodMapping(ctx.Apply,
[]string{"apply"},
[][2]string{},
)
ns.AddMethodMapping(ctx.Delimit,
[]string{"delimit"},
[][2]string{
{`{{ delimit (slice "A" "B" "C") ", " " and " }}`, `A, B and C`},
},
)
ns.AddMethodMapping(ctx.Dictionary,
[]string{"dict"},
[][2]string{},
)
ns.AddMethodMapping(ctx.EchoParam,
[]string{"echoParam"},
[][2]string{
{`{{ echoParam .Params "langCode" }}`, `en`},
},
)
ns.AddMethodMapping(ctx.First,
[]string{"first"},
[][2]string{},
)
ns.AddMethodMapping(ctx.In,
[]string{"in"},
[][2]string{
{`{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}`, `Substring found!`},
},
)
ns.AddMethodMapping(ctx.Index,
[]string{"index"},
[][2]string{},
)
ns.AddMethodMapping(ctx.Intersect,
[]string{"intersect"},
[][2]string{},
)
ns.AddMethodMapping(ctx.IsSet,
[]string{"isSet", "isset"},
[][2]string{},
)
ns.AddMethodMapping(ctx.Last,
[]string{"last"},
[][2]string{},
)
ns.AddMethodMapping(ctx.Querify,
[]string{"querify"},
[][2]string{
{
`{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
`bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`},
{
`<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
`<a href="https://www.google.com?page=3&amp;q=test">Search</a>`},
},
)
ns.AddMethodMapping(ctx.Shuffle,
[]string{"shuffle"},
[][2]string{},
)
ns.AddMethodMapping(ctx.Slice,
[]string{"slice"},
[][2]string{
{`{{ slice "B" "C" "A" | sort }}`, `[A B C]`},
},
)
ns.AddMethodMapping(ctx.Sort,
[]string{"sort"},
[][2]string{},
)
ns.AddMethodMapping(ctx.Union,
[]string{"union"},
[][2]string{
{`{{ union (slice 1 2 3) (slice 3 4 5) }}`, `[1 2 3 4 5]`},
},
)
ns.AddMethodMapping(ctx.Where,
[]string{"where"},
[][2]string{},
)
ns.AddMethodMapping(ctx.Seq,
[]string{"seq"},
[][2]string{
{`{{ seq 3 }}`, `[1 2 3]`},
},
)
return ns
}
internal.AddTemplateFuncsNamespace(f)