dustjs icon indicating copy to clipboard operation
dustjs copied to clipboard

Whitespace-collapsing in templates can lead to no space between a word and a field

Open rjray opened this issue 10 years ago • 8 comments

Using the following template:

{! :tabSize=2:indentSize=2:mode=html: !}
{! The form for help/about !}
<div id="help-box" style="padding:10px">
  <h1>Help / About</h1>
  <form id="help-form">
    <fieldset style="width:100%">
      <div class="formelt">
        <p>
          This is version {version} of the interface, with version
          {backend_version} of the <code>browselogs.pl</code> back-end.
        </p>
      </div>
      <div class="formelt">
        <input type="submit" name="close" value="Close" class="button" style="width:100%">
      </div>
    </fieldset>
  </form>
</div>

When rendered, there is no space between the word "version" and the filled-in value of the field {backend_version}. The collapsing of whitespace seems to have been a little too eager in this case.

rjray avatar Mar 18 '14 18:03 rjray

Forgot: This is seen in version 2.2.8.

rjray avatar Mar 18 '14 18:03 rjray

Interesting-- I just modified the template to get around this, and found that the problem isn't with fields at all, it's with multi-line element content. I changed the <p> block to be:

        <p>
          This is version {version} of the interface, with
          version {backend_version} of the <code>browselogs.pl</code> back-end.
        </p>

As a result, the words "with" and "version" are now run together.

rjray avatar Mar 18 '14 18:03 rjray

Yep, dust aggresively strips whitespace which can bite you in a number of ways, particularly the end of line removal. This 2.3.3 feature might help. {xxxx} {Preserve all new lines, whitespace, and braces} - Available with 2.3.3 release

There are other open issues in this area and solutions are being investigated.

rragan avatar Mar 18 '14 18:03 rragan

Is this feature documented somewhere? I can't find it in the wiki pages.

rjray avatar Mar 18 '14 19:03 rjray

Should be there. I copied the "Preserve ..." text above from the wiki

rragan avatar Mar 18 '14 20:03 rragan

Also the syntax is {content}

But the content won't evaluate any dust syntax within it. It will be read as is.

prashn64 avatar Mar 18 '14 20:03 prashn64

Ahhh... bitten by GitHub markdown formatting-- I didn't note the presence of the back-ticks in the original comment.

I can live (for now) with this construct, as only the one paragraph should have any dust syntax in it, and that one I'll just not wrap (for now).

rjray avatar Mar 18 '14 20:03 rjray

A few solutions to this exist now. The easiest option is just to add a trailing space at the end of the first line. Testing with 2.5.1, the space is maintained. Or, you could always add a {~s} or a {~n} after the first line to force a space or new line.

Or, alternatively, since 2.5.0 you can use dust.config = { whitespace: true }; to maintain white space. (See: https://github.com/linkedin/dustjs/issues/238)

BradEstey avatar Jan 02 '15 04:01 BradEstey