dustjs
dustjs copied to clipboard
Whitespace-collapsing in templates can lead to no space between a word and a field
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.
Forgot: This is seen in version 2.2.8.
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.
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.
Is this feature documented somewhere? I can't find it in the wiki pages.
Should be there. I copied the "Preserve ..." text above from the wiki
Also the syntax is {content
}
But the content won't evaluate any dust syntax within it. It will be read as is.
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).
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)