Skip to content
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

Open
mxmlndml opened this issue Feb 26, 2025 · 0 comments
Open

templ fmt cuts off necessary whitespace #1080

mxmlndml opened this issue Feb 26, 2025 · 0 comments

Comments

@mxmlndml
Copy link

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

templ Link(href string) {
	<a href={ templ.URL(href) }>
		{ children... }
	</a>
}

templ Index() {
	<p>
		@Link("https://templ.guide") {
			This
		}
		 is my favorite website
	</p>
}

After running templ fmt ., the formatting gets changed to this:

package main

templ Link(href string) {
	<a href={ templ.URL(href) }>
		{ children... }
	</a>
}

templ Index() {
	<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><a href="https://templ.guide">This</a>is my favorite website</p>

instead of this:

<p><a href="https://templ.guide">This</a> is my favorite website</p>

Side-by-side comparison in the browser:
Trimmed and untrimmed whitespace

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:

  1. use HTML entities:
templ Index() {
	<p>
		@Link("https://templ.guide") {
			This
		}
		&nbsp;is my favorite website
	</p>
}

However, this leads to different textual output but will get rendered correctly in a browser.

  1. use an empty expression:
templ Index() {
	<p>
		@Link("https://templ.guide") {
			This
		}
		{ `` } is my favorite website
	</p>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant