Debian Codegraph @

tpl/templates/init.go

raw
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package templates
15
16 import (
17 "context"
18
19 "github.com/gohugoio/hugo/deps"
20 "github.com/gohugoio/hugo/tpl/internal"
21 "github.com/gohugoio/hugo/tpl/partials"
22 )
23
24 const name = "templates"
25
26 func init() {
27 f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
28 ctx := New(d)
29
30 ns := &internal.TemplateFuncsNamespace{
31 Name: name,
32 Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil },
33 OnCreated: func(m map[string]any) {
34 LOOP:
35 for _, v := range m {
36 switch v := v.(type) {
37 case *partials.Namespace:
38 ctx.partialsNs = v
39 break LOOP
40 }
41 }
42 if ctx.partialsNs == nil {
43 panic("partialsNs namespace not found")
44 }
45 },
46 }
47
48 ns.AddMethodMapping(ctx.Current,
49 nil,
50 [][2]string{},
51 )
52
53 ns.AddMethodMapping(ctx.Defer,
54 nil, // No aliases to keep the AST parsing simple.
55 [][2]string{},
56 )
57
58 // For internal use only.
59 ns.AddMethodMapping(ctx.DoDefer,
60 []string{"doDefer"},
61 [][2]string{},
62 )
63
64 ns.AddMethodMapping(ctx.Inner,
65 []string{"inner"},
66 [][2]string{},
67 )
68
69 // For internal use only.
70 ns.AddMethodMapping(ctx._PushPartialDecorator,
71 []string{"_pushPartialDecorator"},
72 [][2]string{},
73 )
74
75 // For internal use only.
76 ns.AddMethodMapping(ctx._PopPartialDecorator,
77 []string{"_popPartialDecorator"},
78 [][2]string{},
79 )
80
81 ns.AddMethodMapping(ctx.Exists,
82 nil,
83 [][2]string{
84 {`{{ if (templates.Exists "_partials/header.html") }}Yes!{{ end }}`, `Yes!`},
85 {`{{ if not (templates.Exists "_partials/doesnotexist.html") }}No!{{ end }}`, `No!`},
86 },
87 )
88
89 return ns
90 }
91
92 internal.AddTemplateFuncsNamespace(f)
93 }