Best practice with trimming white space

Curious to find out what people are doing:

  • Do you add dashes, like so {{- function -}} or not?

Is there some form of best practice or is this entirely what one prefers? The reason I ask is that I personally don’t do this, but I’ve noticed that the minify operation does not remove all white space. I tried add dashes to all code and now nearly all white space is removed.

1 Like

This is not a matter of preference. Trimming whitespace in Go templates is only possible with hyphens.

This I understand. I should have perhaps worded my question better.

It’s just that when I look at Hugo projects that are open source, the ones I’ve comes across, don’t seem to use hyphens.

As an example, here’s a link to a Hugo docs layout file from the Github repo.

In the code, the author has chosen not to do {{- -}} in the template. I’m trying to understand if the hypens should be used throughout a project whether there is an issue with whitespace or not.

Have I managed to explain myself better?

Unless you write your templates without newlines there will be whitespace in the output a Go template.

Some people care about whitespace because it interferes with the design of their pages, while others may have other objectives and may not pay attention to such matters.

Personally I use the hyphens all the time, since I am a designer and layout is very important to me.

1 Like

I see. Thank you for the explanation.

You can choose to use it wherever you prefer. In fact, you are not even forced to use them in pairs, as {{- printf "%s" hello"}} removes leading whitespace, but not trailing.

The issue with not using them is that your rendered template is that you get an ugly-looking html text file, filled with empty lines where the blocks used to be. I find that text files without too many newlines are much easier to debug because you don’t have to scroll to much to move around.

But HTML largely ignores whitespace so again, it’s a matter of preference.

1 Like

However as stated in that link further below:

Any whitespace characters that are outside of HTML elements in the original document are represented in the DOM. This is needed internally so that the editor can preserve formatting of documents. This means that:

There will be some text nodes that contain only whitespace, and
Some text nodes will have whitespace at the beginning or end.

In this scenario one may encounter layout problems as I mentioned above.
Before the hyphens syntax was made available in Go 1.6 this forum had several topics from users asking questions about layout problems due to whitespace.

2 Likes

Yeah, people need to understand how HTML whitespace and minification works and how browsers are handling spaces :wink: Two spaces are rendered as one space. But if the space is after a closing HTML tag and is followed by a new line before another tag is added, then most minification procedures remove EVERTHING between the tags.

While developing I don’t use dashes so I can debug better while watching the source code. Then when the layout is more or less set in stone I add dashes, so that the code collapses and I don’t need to scroll too much when I am debugging.

Then, after minification I compare dev site with live site. Often accidental missing spaces are found then (for instance between icons-font-icons and text) and I fix them with CSS, NOT forced whitespace $nbsp; :wink:

A “space” character (tap on the space bar) should NEVER be used for layout purposes. Once one forces themself to keep that dogma all the little spacing issues disappear.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.