File: tags_templatetag.go

package info (click to toggle)
golang-github-flosch-pongo2.v4 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, trixie
  • size: 860 kB
  • sloc: makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,079 bytes parent folder | download | duplicates (2)
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
package pongo2

type tagTemplateTagNode struct {
	content string
}

var templateTagMapping = map[string]string{
	"openblock":     "{%",
	"closeblock":    "%}",
	"openvariable":  "{{",
	"closevariable": "}}",
	"openbrace":     "{",
	"closebrace":    "}",
	"opencomment":   "{#",
	"closecomment":  "#}",
}

func (node *tagTemplateTagNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
	writer.WriteString(node.content)
	return nil
}

func tagTemplateTagParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
	ttNode := &tagTemplateTagNode{}

	if argToken := arguments.MatchType(TokenIdentifier); argToken != nil {
		output, found := templateTagMapping[argToken.Val]
		if !found {
			return nil, arguments.Error("Argument not found", argToken)
		}
		ttNode.content = output
	} else {
		return nil, arguments.Error("Identifier expected.", nil)
	}

	if arguments.Remaining() > 0 {
		return nil, arguments.Error("Malformed templatetag-tag argument.", nil)
	}

	return ttNode, nil
}

func init() {
	RegisterTag("templatetag", tagTemplateTagParser)
}