diff --git a/context.go b/context.go index 525ac3a..82b9330 100644 --- a/context.go +++ b/context.go @@ -71,6 +71,8 @@ type ExecutionContext struct { Public Context Private Context Shared Context + + DisallowNotExistedVar bool } var pongo2MetaContext = Context{ diff --git a/options.go b/options.go index 9c39e46..1550a61 100644 --- a/options.go +++ b/options.go @@ -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 { diff --git a/template.go b/template.go index f96b3f4..a09574d 100644 --- a/template.go +++ b/template.go @@ -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 } diff --git a/variable.go b/variable.go index 96e047e..8973480 100644 --- a/variable.go +++ b/variable.go @@ -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 }