...
1 package statements
2
3 import (
4 "fmt"
5
6 "github.com/pkg/errors"
7
8 "github.com/noirbizarre/gonja/exec"
9 "github.com/noirbizarre/gonja/nodes"
10 "github.com/noirbizarre/gonja/parser"
11 "github.com/noirbizarre/gonja/tokens"
12 )
13
14 type ExtendsStmt struct {
15 Location *tokens.Token
16 Filename string
17 WithContext bool
18 }
19
20 func (stmt *ExtendsStmt) Position() *tokens.Token { return stmt.Location }
21 func (stmt *ExtendsStmt) String() string {
22 t := stmt.Position()
23 return fmt.Sprintf("ExtendsStmt(Filename=%s Line=%d Col=%d)", stmt.Filename, t.Line, t.Col)
24 }
25
26 func (node *ExtendsStmt) Execute(r *exec.Renderer) error {
27 return nil
28 }
29
30 func extendsParser(p *parser.Parser, args *parser.Parser) (nodes.Statement, error) {
31 stmt := &ExtendsStmt{
32 Location: p.Current(),
33 }
34
35 if p.Level > 1 {
36 return nil, args.Error(`The 'extends' statement can only be defined at root level`, p.Current())
37 }
38
39 if p.Template.Parent != nil {
40 return nil, args.Error("This template has already one parent.", args.Current())
41 }
42
43
44 if filename := args.Match(tokens.String); filename != nil {
45 stmt.Filename = filename.Val
46 tpl, err := p.TemplateParser(stmt.Filename)
47 if err != nil {
48 return nil, errors.Wrapf(err, `Unable to parse parent template '%s'`, stmt.Filename)
49 }
50 p.Template.Parent = tpl
51 } else {
52 return nil, args.Error("Tag 'extends' requires a template filename as string.", args.Current())
53 }
54
55 if tok := args.MatchName("with", "without"); tok != nil {
56 if args.MatchName("context") != nil {
57 stmt.WithContext = tok.Val == "with"
58 } else {
59 args.Stream.Backup()
60 }
61 }
62
63 if !args.End() {
64 return nil, args.Error("Tag 'extends' does only take 1 argument.", nil)
65 }
66
67 return stmt, nil
68 }
69
70 func init() {
71 All.Register("extends", extendsParser)
72 }
73
View as plain text