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

fix: maintain whitespace around templated values #156

Merged
merged 1 commit into from
Sep 27, 2023
Merged

Conversation

noaoh
Copy link
Contributor

@noaoh noaoh commented Sep 17, 2023

I noticed while following the docs (specifically https://templ.guide/syntax-and-usage/expressions) that for the following template:

package main

templ greet(prefix string, p Person) {
  <div>{ prefix } { p.Name }{ exclamation }</div>
}

The whitespace between {prefix} and {p.Name} is not preserved. This is an attempt to fix that.

@a-h
Copy link
Owner

a-h commented Sep 17, 2023

You're absolutely right. Good spot, thanks for that! I checked and saw that people have been putting in &nbsp; at work.

This is much better!

@a-h
Copy link
Owner

a-h commented Sep 19, 2023

I took a look at this yesterday, it does look great, but I realised that the templ fmt removes whitespace between templated values, so I wanted to think about this change and if/how it interacts with templ fmt, hence the pause.

There's an open issue around the formatting behaviour which I need to revisit. I've had a few attempts at it, but haven't managed to get something I'm 100% happy with yet.

From discussion with @joerdav, I think the way we might go with formatting is simply to add additional indentation where it's missing, but do nothing more than that - i.e. no whitespace removal, and then on generation, normalise whitespace according to HTML rules (i.e. collapse runs of whitespace down to a single space).

@noaoh
Copy link
Contributor Author

noaoh commented Sep 20, 2023

My thought would be that formatting should remove extra whitespace where it's found, but leave a single character of whitespace. This is so there's no differences between the formatted template, and what gets generated. Just my two cents.

@joerdav
Copy link
Collaborator

joerdav commented Sep 21, 2023

@noaoh the difficulty being that it seems everyone wants to format their HTML differently in templ, some want all their elements to be displayed on new lines similar to prettier, others want some elements to be inlined and not others.

So I think the best medium is that templ fmt doesn't destroy any whitespace, but maybe can add some in cases where the intent is obvious, like turning the following into a block element:

<!-- input -->
<div>hello
</div>
<!-- becomes -->
<div>
    hello
</div>

@a-h a-h merged commit 92d3cd1 into a-h:main Sep 27, 2023
3 checks passed
@a-h
Copy link
Owner

a-h commented Sep 27, 2023

Great job, thanks for sorting that out - it's much appreciated.

The formatting changes were just fixed, and this PR worked perfectly in conjunction.

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

Successfully merging this pull request may close these issues.

3 participants