For example:
{{range .Users}}
{{if .IsAdmin}}
{{/* How to use "break" or "continue"? */}}
{{end}}
{{end}}
The documentation for "break" or "continue" in templates is not available in golang.org
For example:
{{range .Users}}
{{if .IsAdmin}}
{{/* How to use "break" or "continue"? */}}
{{end}}
{{end}}
The documentation for "break" or "continue" in templates is not available in golang.org
They are not documented because they do not exist.
To make sure - check the tests for the text/template
lexer: https://github.com/golang/go/blob/master/src/text/template/parse/lex_test.go
break
, and you don't need continue
as long as you have {{if
. Golang templates are not suited to have too complicated logic. –
Tippet {{if .IsAdmin}}
. What is inside the {{if }}
would only be rendered if the condition is true. –
Tippet Go101 does mention (May 2021, 4+ years later):
Since Go 1.18, "break" and "continue" might be supported in range loops of Go templates.
Note: Go 1.18 should be released in Q1 2022.
That would solve issue 20531 text/template: add break
and continue
support.
And is currently implemented in CL 321491: html/template
, text/template
: implement break
and continue
for range
loops.
This is still a work in progress for now (Q1 2021)
Update Sept. 2021: confirmed, with commit d0dd26a
html/template
,text/template
: implementbreak
andcontinue
forrange
loops
break
andcontinue
for range loops was accepted as a proposal in June 2017.
It was implemented in CL 66410 (Oct 2017)
but then rolled back in CL 92155 (Feb 2018) becausehtml/template
changes had not been implemented.This CL reimplements break and continue in
text/template
and then adds support for them in html/template as well.
break
and continue
statements are part of text/template
and html/template
in Go 1.10 (in Beta at time of writing). From the release notes:
The new actions
{{break}}
and{{continue}}
break out of the innermost{{range ...}}
loop, like the corresponding Go statements.
Prior releases of Go (Before 1.10) do not support break
or continue
statements.
Looking at the beta documentation you can see the new itemContinue
and itemBreak
items in the lexer, the new nodes like ContinueNode
in Parser to follow the code.
They are not documented because they do not exist.
To make sure - check the tests for the text/template
lexer: https://github.com/golang/go/blob/master/src/text/template/parse/lex_test.go
break
, and you don't need continue
as long as you have {{if
. Golang templates are not suited to have too complicated logic. –
Tippet {{if .IsAdmin}}
. What is inside the {{if }}
would only be rendered if the condition is true. –
Tippet You can use a variable to add a in processing check and skip any existing checks for the remainder of the loop.
// method 1
{{ $dothing := false }}
{{ range $i, $A := .List }}
{{ if $A.Check }}
{{ $dothing = true }}
{{end}}
{{ end }}
{{ if $dothing }}
// do thing
{{ end }}
// method 2
{{ $already := false }}
{{ range $i, $A := .List }}
{{ if $already }}
{{ else }}
{{ if $A.Check }}
{{ $already = true }}
// do thing
{{ end }}
{{ end }}
{{ end }}
© 2022 - 2024 — McMap. All rights reserved.