Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars.net not preserving white space

I have the following template:

<!DOCTYPE html>
<html>
<body>
    {{>Header}}
    This is a test template.
    {{>Footer}}
</body>
</html>

When I compile this template, I'd expect to get this:

<!DOCTYPE html>
<html>
<body>
    This is a header.
    This is a test template.
    This is a footer.
</body>
</html>

Instead, what I get is this:

<!DOCTYPE html>
<html>
<body>
This is a header.    This is a test template.
This is a footer.</body>
</html>

The indentations before the first and third lines are gone; and the newlines before the second line and closing body tag are gone. Is this expected, and is there a way to preserve the whitespace just as it is laid out in the base template? I should note that I'm using Handlebars.Net here, although my understanding is that it's meant to emulate the original Javascript spec as closely as possible.

like image 492
user3678429 Avatar asked Dec 30 '25 18:12

user3678429


1 Answers

(Answer from the Github issue where this was also posted):

So there's two different things going on here that I'll summarize first and then explain: 1) what you expect is incorrect; 2) what you are getting is also incorrect, in a different way:

  1. Whitespace (and line breaks) are not significant in HTML, and though Handlebars is technically a general string templating language, the design decisions & opinions it contains are heavily slanted towards using it for an HTML templating language. You should not expect it to preserve implicit line breaks, only explicit line breaks (e.g. if you put \n it will preserve that)

  2. Handlebars.Net actually DOES preserve some line breaks when it's not supposed to! That's a bug but one many users are currently relying on, so we'll keep it in 1.x but fix it in v2.

To get your desired output, put explicit line breaks in your template. Cheers!

like image 153
Rex M Avatar answered Jan 01 '26 09:01

Rex M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!