Skip to content

Early return from templ component? #934

Answered by pharrisee
ltdangle asked this question in Q&A
Discussion options

You must be logged in to vote

a little late to the party but I've just tried this:

templ Index() {
	<h1 class="text-4xl font-semibold">Index</h1>
	<div>
		<h2 class="text-3xl mb-4">Buttons</h2>
	</div>
	if true {
		{{ return }}
	}
	<dialog id="the-dialog" class="rounded"></dialog>
}

And as would be hoped the dialog is not rendered.

I'm still not sure that this would be something I'd expect to see in a real code base personally, since I'm not sure what side-effects there may be to returning early. I can only imagine tag endings being missing but there's probably more side-effects that I'm not imagining.

This relies on the Raw Go experimental functionality.

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by ltdangle
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@roborobs1023
Comment options

@a-h
Comment options

a-h Oct 30, 2024
Maintainer

@ltdangle
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants