leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.
MIT License
648 stars 159 forks source link

Unwanted newline #81

Open zorbash opened 11 years ago

zorbash commented 11 years ago

When using the require directive to embed a template file in another file the initial template source is altered (a newline is appended).

It may seem harmless but it may cause undesired and visible effects.

Example.

# base.js.coffee
#= require templates/foo

text = document.createTextNode(JST['foo']())
el = document.getElementById('container')
el.appendChild(text)
# templates/foo
no newline after this phrase

In the example above the text node will be

"no newline after this phrase
"

instead of "no newline after this phrase"

We have currently decided to trim the compiled template output before using it with text nodes. But is there another way around?

zorbash commented 11 years ago

The code causing the above is https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/directive_processor.rb#L80

AlexRiedler commented 11 years ago

I suppose you could trim it in handlebars_assets as well, but that might also cause unexpected results (in certain cases). This is probably a really rare case though.

why not just do <p>no new line after this phrase</p> ; which fixes it for your example I believe ? potentially? but maybe your toy example does not actually fully clarify the issue.

zorbash commented 11 years ago

I agree that adding specific markup in a template may bypass the problem. But the thing here is that the template source is altered in a way that the same template source would yield different output when compiled in the browser.

AlexRiedler commented 10 years ago

So... I would love to just strip the whitespace, however in the case that someone ACTUALLY wants the whitespace then it would break that case... I also do not want to re-grab the body that tilt has already grabbed... hmmm this will plague all template compilers.

However there is a simple work-around in the case the user wants the whitespace (add two-newlines).

@Zorbash, Agree with the trim at most one new-line approach?

zorbash commented 10 years ago

@AlexRiedler Yes, if someone (for compatibility reasons perhaps) still wants the extra newline, two newlines should be inserted for this newline to be appended. Are you on it?

AlexRiedler commented 10 years ago

Yup, on it! (I think it may already be done on my branch; but still needs some verification with how HAML and SLIM works in this case).