Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ type ExecutionContext struct {
Public Context
Private Context
Shared Context

DisallowNotExistedVar bool
}

var pongo2MetaContext = Context{
Expand Down
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type Options struct {

// If this is set to true leading spaces and tabs are stripped from the start of a line to a block. Defaults to false
LStripBlocks bool

// If this is set to true, the Execute() would returns error when var not exists
DisallowNotExistedVar bool
}

func newOptions() *Options {
Expand Down
1 change: 1 addition & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (tpl *Template) newContextForExecution(context Context) (*Template, *Execut

// Create operational context
ctx := newExecutionContext(parent, newContext)
ctx.DisallowNotExistedVar = tpl.Options.DisallowNotExistedVar

return parent, ctx, nil
}
Expand Down
3 changes: 3 additions & 0 deletions variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ func (vr *variableResolver) resolve(ctx *ExecutionContext) (*Value, error) {

if !current.IsValid() {
// Value is not valid (anymore)
if ctx.DisallowNotExistedVar {
return nil, fmt.Errorf("invalid identifier %s", vr.String())
}
return AsValue(nil), nil
}

Expand Down