Alexander
Alexander

Reputation: 7832

dust.js include partitial without trimming

I am using dust.js template engine. The main template includes a partial and the problem is that dust.js trims every line of the included template file.

For example, the main template is:

<h1>{header}</h1>
{>dust_cnt /}

The dust_cnt.dust is:

<div>
  This is
  the content
</div>

And I render the response by: res.render('dust_template.dust', {header: 'TEST OK'});

The problem that there is no space between This is and the content in the output. So the output looks like:

TEST OK

This isthe content

I can't change all the contents by putting {~n} or another separator and I don't think there is any reason to do that.

On http://linkedin.github.com/dustjs/ I found the following remark GH- 166 - Added trimming of white spaces in dust templates configuration in the dust.compile ( default option is still true). But can't find the way to set set the options to avoid the trimming?

How can I make dust.js to render it the way I expect (with the spaces)?

Thank you in advance!

Upvotes: 2

Views: 812

Answers (2)

Alexander
Alexander

Reputation: 7832

Ok, I found the solution :). After digging in the source code + checking the issues on the dust's GitHub I decided to use the following to prevent the formatting of the template's nodes:

dust.optimizers.format = function(ctx, node) { return node };

(Also on https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#controlling-whitespace-suppression)

The default behavior is very dangerous - as it mentioned on the GitHub it can cause serious problems, for example:

<script>
//this will alert "test"
alert("this is test");
</script>

Will be rendered as:

<script>//this will alert "test" alert("this is test");</script>

Upvotes: 3

Sebastian vom Meer
Sebastian vom Meer

Reputation: 5241

This is strange and I understand that you don't want to edit your template files. However, you can workaround this in a less ugly way by adding a space at the end of the line:

<div>
  This is < add space here
  the content
</div>

Just for the case you cannot find a real solution for this.

Upvotes: 0

Related Questions