-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
There is currently no good way to cleanly stop template execution early.
I propose that we add a return action that stops execution of the current template, without error. When using nested templates, the calling template should keep executing. That is, return should be scoped to the current template.
This can be partially implemented with a simple FuncMap entry and a (custom) sentinel error:
"return": func() (string, error) {
return "", ErrStop
}The Go code executing the template can then recognize ErrStop and ignore it. However, this implementation stops the entire template execution, including caller (parent) templates. One can write an alternative FuncMap implementation of the template action that handles ErrStop. But that's a lot of machinery, and you must buffer the intermediate template result.
The other alternative is to wrap large chunks of templates with if blocks, which is bad for readability.
Given that Go style encourages early exits, the template package ought to support it.
Metadata
Metadata
Assignees
Type
Projects
Status