mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-26 22:04:32 +02:00
tpl: Add docshelper for template funcs
And fix some other minor related issues. Updates #3418
This commit is contained in:
@@ -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&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&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)
|
||||
|
Reference in New Issue
Block a user