File: tags_block.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 (129 lines) | stat: -rw-r--r-- 3,114 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package pongo2

import (
	"bytes"
	"fmt"
)

type tagBlockNode struct {
	name string
}

func (node *tagBlockNode) getBlockWrappers(tpl *Template) []*NodeWrapper {
	nodeWrappers := make([]*NodeWrapper, 0)
	var t *NodeWrapper

	for tpl != nil {
		t = tpl.blocks[node.name]
		if t != nil {
			nodeWrappers = append(nodeWrappers, t)
		}
		tpl = tpl.child
	}

	return nodeWrappers
}

func (node *tagBlockNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
	tpl := ctx.template
	if tpl == nil {
		panic("internal error: tpl == nil")
	}

	// Determine the block to execute
	blockWrappers := node.getBlockWrappers(tpl)
	lenBlockWrappers := len(blockWrappers)

	if lenBlockWrappers == 0 {
		return ctx.Error("internal error: len(block_wrappers) == 0 in tagBlockNode.Execute()", nil)
	}

	blockWrapper := blockWrappers[lenBlockWrappers-1]
	ctx.Private["block"] = tagBlockInformation{
		ctx:      ctx,
		wrappers: blockWrappers[0 : lenBlockWrappers-1],
	}
	err := blockWrapper.Execute(ctx, writer)
	if err != nil {
		return err
	}

	return nil
}

type tagBlockInformation struct {
	ctx      *ExecutionContext
	wrappers []*NodeWrapper
}

func (t tagBlockInformation) Super() string {
	lenWrappers := len(t.wrappers)

	if lenWrappers == 0 {
		return ""
	}

	superCtx := NewChildExecutionContext(t.ctx)
	superCtx.Private["block"] = tagBlockInformation{
		ctx:      t.ctx,
		wrappers: t.wrappers[0 : lenWrappers-1],
	}

	blockWrapper := t.wrappers[lenWrappers-1]
	buf := bytes.NewBufferString("")
	err := blockWrapper.Execute(superCtx, &templateWriter{buf})
	if err != nil {
		return ""
	}
	return buf.String()
}

func tagBlockParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
	if arguments.Count() == 0 {
		return nil, arguments.Error("Tag 'block' requires an identifier.", nil)
	}

	nameToken := arguments.MatchType(TokenIdentifier)
	if nameToken == nil {
		return nil, arguments.Error("First argument for tag 'block' must be an identifier.", nil)
	}

	if arguments.Remaining() != 0 {
		return nil, arguments.Error("Tag 'block' takes exactly 1 argument (an identifier).", nil)
	}

	wrapper, endtagargs, err := doc.WrapUntilTag("endblock")
	if err != nil {
		return nil, err
	}
	if endtagargs.Remaining() > 0 {
		endtagnameToken := endtagargs.MatchType(TokenIdentifier)
		if endtagnameToken != nil {
			if endtagnameToken.Val != nameToken.Val {
				return nil, endtagargs.Error(fmt.Sprintf("Name for 'endblock' must equal to 'block'-tag's name ('%s' != '%s').",
					nameToken.Val, endtagnameToken.Val), nil)
			}
		}

		if endtagnameToken == nil || endtagargs.Remaining() > 0 {
			return nil, endtagargs.Error("Either no or only one argument (identifier) allowed for 'endblock'.", nil)
		}
	}

	tpl := doc.template
	if tpl == nil {
		panic("internal error: tpl == nil")
	}
	_, hasBlock := tpl.blocks[nameToken.Val]
	if !hasBlock {
		tpl.blocks[nameToken.Val] = wrapper
	} else {
		return nil, arguments.Error(fmt.Sprintf("Block named '%s' already defined", nameToken.Val), nil)
	}

	return &tagBlockNode{name: nameToken.Val}, nil
}

func init() {
	RegisterTag("block", tagBlockParser)
}