You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
package main
templLink(href string) {
<ahref={ templ.URL(href) }>
{ children... }
</a>
}
templIndex() {
<p>
@Link("https://templ.guide") {
This
}
is my favorite website
</p>
}
After running templ fmt ., the formatting gets changed to this:
package main
templLink(href string) {
<ahref={ templ.URL(href) }>
{ children... }
</a>
}
templIndex() {
<p>
@Link("https://templ.guide") {
This
}
is my favorite website
</p>
}
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:
<p><ahref="https://templ.guide">This</a>is my favorite website</p>
instead of this:
<p><ahref="https://templ.guide">This</a> is my favorite website</p>
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):
OS: macOS
templ CLI version v0.3.833
Go version go1.24.0 darwin/arm64
gopls version golang.org/x/tools/gopls v0.18.1
Additional context
This issue is not restricted to templ fmt. Running templ generate on an unformatted .templ file yields the same formatting issue. I suspect during generation whitespace is trimmed in a similar manner as with templ fmt.
I came up with the following workarounds for the moment:
use HTML entities:
templIndex() {
<p>
@Link("https://templ.guide") {
This
}
is my favorite website
</p>
}
However, this leads to different textual output but will get rendered correctly in a browser.
use an empty expression:
templIndex() {
<p>
@Link("https://templ.guide") {
This
}
{ `` } is my favorite website
</p>
}
The text was updated successfully, but these errors were encountered:
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: