File: tags_firstof.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 (49 lines) | stat: -rw-r--r-- 926 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
46
47
48
49
package pongo2

type tagFirstofNode struct {
	position *Token
	args     []IEvaluator
}

func (node *tagFirstofNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
	for _, arg := range node.args {
		val, err := arg.Evaluate(ctx)
		if err != nil {
			return err
		}

		if val.IsTrue() {
			if ctx.Autoescape && !arg.FilterApplied("safe") {
				val, err = ApplyFilter("escape", val, nil)
				if err != nil {
					return err
				}
			}

			writer.WriteString(val.String())
			return nil
		}
	}

	return nil
}

func tagFirstofParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
	firstofNode := &tagFirstofNode{
		position: start,
	}

	for arguments.Remaining() > 0 {
		node, err := arguments.ParseExpression()
		if err != nil {
			return nil, err
		}
		firstofNode.args = append(firstofNode.args, node)
	}

	return firstofNode, nil
}

func init() {
	RegisterTag("firstof", tagFirstofParser)
}