songyy
songyy

Reputation: 4563

Config DustJS (ServerSide) do not escape new line?

I'm using DustJS with ExpressJS with NodeJS.

When ExpressJS renders, it auto escape the new line and white space in my dustJS; while since I'm putting JavaScript into my DustJS templates, I want to leave the white spaces as it is.

How to achieve that?

Thanks a lot.

Update: here's some further explanation:

In my template.dust, I have something like:

 <script type='template' id="tempVO">{.vo|s}</script>
 <script>  {! Block to compile the dustJS into cache !}
 $(function($){
   dust.compile($("#tempVO").html(),"vo");

   // register helper
   dust.helpers.justToString = function(chunk, ctx, body, param){
     console.log(param);
   }
 })();

The default behaviour of DustJS, when used with expressJS by doing:

app.get("/",function(req,res){
  res.render("ajaxTemplate",xmlService.templates);
});

would escape all the white spaces so that it turns out something like:

 $(function($){dust.compile($("#tempVO").html(),"vo");// register helperdust.helpers.justToString = function(chunk, ctx, body, param){console.log(param);}})();// AJAX Callback</script><script>$(function(){$(".tipNeeded").tooltip();$(".tabsNeeded").tabs();});

Which is certainly not what I want.]

Although I can just use some external JavaScript Import, like:

  <script src="my/own/beautiful.js"></script>

Still I'd like to know how to put in-HTML script by not-escaping the white space when render my dustJS files.

Upvotes: 0

Views: 565

Answers (1)

Matthew Bakaitis
Matthew Bakaitis

Reputation: 11990

From the Dust Tutorial on controlling whitespace suppression, the exact method would vary based upon the version you are using. If you are on v1.2.6 (the latest as of this writing) you would add this line to your code:

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

To quickly test it, add that line immediately after your require for the dustjs-linkedin module:

var dust = require('dustjs-linkedin');

Edit:

From version 1.2.0 to 1.2.5, the compile function took three arguments:

dust.compile = function(source, name, strip)

If strip was set to false then whitespace would be preserved. However, it looks like this argument was removed in 1.2.6. Even with a version that has the strip argument, when using express, you aren't calling dust.compile directly. You might be able to use something like consolidate.js as a basis to write two render calls, one with whitespace and one without...but I don't see a plainly simple way.

Upvotes: 1

Related Questions