How to use continue and break keywords in golang templates?
Asked Answered
M

4

4

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

Mcgannon answered 19/4, 2017 at 2:29 Comment(1)
Confirmed for Go 1.18 (Q4 2021). See my answer belowMontage
T
4

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

Tippet answered 19/4, 2017 at 2:34 Comment(5)
But how can I accomplish this is go? Is there other way?Mcgannon
You cannot simulate break, and you don't need continue as long as you have {{if. Golang templates are not suited to have too complicated logic.Tippet
Can you share an example on how can I skip or stop an iteration?Mcgannon
You cannot stop an iteration. But you can skip with a simple {{if .IsAdmin}}. What is inside the {{if }} would only be rendered if the condition is true.Tippet
Ok I get it. Thanks.Mcgannon
M
6

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: implement break and continue for range loops

break and continue 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) because html/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.

Montage answered 22/5, 2021 at 20:8 Comment(0)
A
5

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.

Afflict answered 14/12, 2017 at 17:37 Comment(1)
The new break and continue actions do not work in html/template, and fixing them requires thinking about security issues that seem too tricky at this stage of the release. We will try again for 1.11. github.com/golang/go/commit/…Pharyngoscope
T
4

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

Tippet answered 19/4, 2017 at 2:34 Comment(5)
But how can I accomplish this is go? Is there other way?Mcgannon
You cannot simulate break, and you don't need continue as long as you have {{if. Golang templates are not suited to have too complicated logic.Tippet
Can you share an example on how can I skip or stop an iteration?Mcgannon
You cannot stop an iteration. But you can skip with a simple {{if .IsAdmin}}. What is inside the {{if }} would only be rendered if the condition is true.Tippet
Ok I get it. Thanks.Mcgannon
E
1

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 }}


Eisen answered 26/3, 2020 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.