Skip to content

proposal: text/template: add {{break 2}} and {{continue 2}} support #65090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Drugoy opened this issue Jan 13, 2024 · 4 comments
Closed

proposal: text/template: add {{break 2}} and {{continue 2}} support #65090

Drugoy opened this issue Jan 13, 2024 · 4 comments

Comments

@Drugoy
Copy link

Drugoy commented Jan 13, 2024

Proposal Details

Issue #20531 introduced {{break}} and {{continue}} support, but they are currently limited to the innermost {{range}}.
I'd like to be able to {{break}} 2nd level {{range}} from inside an {{if ...}} block of an inner {{range}} block (to stop parsing a bigger list of objects when I've found what I was looking for).

@seankhliao seankhliao changed the title text/template: add {{break 2}} and {{continue 2}} support proposal: text/template: add {{break 2}} and {{continue 2}} support Jan 13, 2024
@gopherbot gopherbot added this to the Proposal milestone Jan 13, 2024
@AlexanderYastrebov
Copy link
Contributor

Currently a workaround is possible using a variable https://go.dev/play/p/Rt9WyUb-tsk:

{{ $done := false }}
{{ range .Lists }}
	{{ range $v := . }}
		{{ $v }}
		{{ if eq $v "bar" }}
			{{ $done = true }}
			{{ break }}
		{{ end }}
	{{ end }}
	{{ if $done }}
		{{ break }}
	{{ end }}
{{ end }}

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Feb 6, 2024
@ianlancetaylor
Copy link
Contributor

CC @robpike

@robpike
Copy link
Contributor

robpike commented Feb 7, 2024

The case arises rarely enough that the workaround seems fine. Templates do not have to support everything from other languages. Also, the numeric form of this (break 2) is famously error-prone. The right solution would involve adding statement labels, and that is a bridge too far.

@Drugoy
Copy link
Author

Drugoy commented Feb 8, 2024

Then 'no go' it is for this proposal.
Thank you.

@Drugoy Drugoy closed this as completed Feb 8, 2024
@golang golang locked and limited conversation to collaborators Feb 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants