-
-
Notifications
You must be signed in to change notification settings - Fork 304
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
templ fmt cuts off necessary whitespace #1080
Comments
Thanks for the bug report. Just leaving some notes. Some of the parsed elements parse trailing whitespace so it can be maintained:
Whereas others just throw any padding away:
I expect the fix here is to apply the same rules as the HTML element stuff - templ/parser/v2/elementparser.go Lines 492 to 515 in f0a1f0c
|
Oh, by the way, the formatter works by parsing the template into an in-memory representation, then writing it out again. e.g. trailing whitespace rules for HTML elements are here: Lines 609 to 617 in f0a1f0c
|
Describe the bug
I have an issue with
templ fmt
with composed templates that contain inline elements. When embedding a different template on a new line, the whitespace before or after the template gets automatically removed by the formatter. See the example below.To Reproduce
Assume, you have this
.templ
file:After running
templ fmt .
, the formatting gets changed to this:Notice how the space in front of "is my favorite website" got removed. This would not be an issue if the Link template would not be an inline element. Now the resulting HTML looks like this:
instead of this:
Side-by-side comparison in the browser:

Expected behavior
templ fmt
should only remove unnecessary whitespace so that the formatting does not cause a difference when rendered by a browser. Specifically in this case,templ fmt
should not remove the space in front of the text or a template.templ info
output(✓) os [ goos=darwin goarch=arm64 ]
(✓) go [ location=/opt/homebrew/bin/go version=go version go1.24.0 darwin/arm64 ]
(✓) gopls [ location=/Users/user/go/bin/gopls version=golang.org/x/tools/gopls v0.18.1 ]
(✓) templ [ location=/Users/user/go/bin/templ version=v0.3.833 ]
Desktop (please complete the following information):
gopls
version golang.org/x/tools/gopls v0.18.1Additional context
This issue is not restricted to
templ fmt
. Runningtempl generate
on an unformatted.templ
file yields the same formatting issue. I suspect during generation whitespace is trimmed in a similar manner as withtempl fmt
.I came up with the following workarounds for the moment:
However, this leads to different textual output but will get rendered correctly in a browser.
The text was updated successfully, but these errors were encountered: