handlebars.js icon indicating copy to clipboard operation
handlebars.js copied to clipboard

After precompilation, templates insert extra spacing

Open JohnDMathis opened this issue 13 years ago • 5 comments

I have a couple of test templates -- "header" and "body". They contain just a div and some text, and are rendered inside regions labeled "header-region" and "body1-region":

image

That's fine, but when I use the precompiled version of the templates, I see extra spacing inserted, throwing off my layout: image

JohnDMathis avatar May 03 '13 14:05 JohnDMathis

While this appears to me to be a valid issue, I found a reasonable workaround for some cases: Instead of specifying a div with class "well well-small" as the first line of my template, I used Backbone's view, and specified the class using the className view property. That works in cases where you have a text node as the first child under your div. However, in many cases my templates use an 'h1" or "h2" as the first line. In this case, it still inserts a blank text node, forcing a new line: image

It looks like it always inserts a blank text line as the first char of a template.

JohnDMathis avatar May 03 '13 15:05 JohnDMathis

I found the issue (sort of) and a true workaround: There is a control character inserted at the head of the template result string. I don't know why, but it's there. For example, in my test case I would expect a string:

<h2>a header</h2>

I would expect the length of this to be 19; 17 chars, plus \r\n. However, the length is actually 20, and the first '<' is at index 1, rather than 0. I fixed it by adding the following after line 2144, right after the result is obtained from the compiled template:

if(result.indexOf('<')===1)
    result = result.substr(1);

I'll look into forking the code and fixing it properly.

JohnDMathis avatar May 03 '13 20:05 JohnDMathis

What does your source file look like if you open it in a hex editor? My first guess would be that your editor inserted a byte order mark.

kpdecker avatar May 10 '13 06:05 kpdecker

I don't think that we want to start altering the content that is passed to us as there are use cases where control characters that the user might want. Closing as this is something that should be handled on a case by case basis although I'm open to a PR that adds a flag to filter this in the bundled command line app if done as a conditional and in a generic manner.

kpdecker avatar May 16 '13 17:05 kpdecker

          <script id="" type="text/x-handlebars-template">
             ....Your template
          </script>

if Script tag is corrupted then also its added a new Line space

rkj1515 avatar Oct 29 '14 08:10 rkj1515