{"id":12264,"date":"2014-03-11T20:43:39","date_gmt":"2014-03-11T15:13:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12264"},"modified":"2014-03-11T20:43:39","modified_gmt":"2014-03-11T15:13:39","slug":"dust-your-views","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/dust-your-views\/","title":{"rendered":"Dust your views"},"content":{"rendered":"<p>In our node js application, we can use variety of templating engines like &#8220;jade&#8221; , &#8220;ejs&#8221;, &#8220;mustache&#8221; etc.<br \/>\n<strong>Dust.js<\/strong> is an asynchronous templating engine which can be used both on server side as well as client side and provide easy caching with high performance.<\/p>\n<p>In my previous blog on <a href=\"http:\/\/www.tothenew.com\/blog\/using-ejs-in-your-node-js-application\/\">EJS as a templating engine<\/a>, i have mentioned how we can use &#8220;ejs&#8221; to render views in an express application. Similarly, we can use <strong>dust<\/strong> for rendering templates as it emphasize on presentation rather than the application and business logic.<\/p>\n<p>To use dust in our application, we need to write this line in our app.js file,<\/p>\n<p>[js]<br \/>\nvar app = express();<br \/>\napp.set(&#8216;view engine&#8217;, &#8216;dust&#8217;);<br \/>\n[\/js]<\/p>\n<p>A sample dust file (demo.dust) will look like:<br \/>\n[js]<br \/>\n&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\nHello {name}!!<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/js]<\/p>\n<p>Now when we render this file and pass some JSON data along with it:<br \/>\n[js]<br \/>\nres.render(&#8216;demo&#8217;, {name: &quot;Sakshi&quot;});<br \/>\n[\/js]<\/p>\n<p>The output will be:<br \/>\n<code>Hello Sakshi!!<\/code><\/p>\n<p>So here we can see, how the passed JSON data is used in the dust file. <\/p>\n<p>Some of the commonly used operators or tags in dust are:<br \/>\n1. { } is used as a replace operator.<br \/>\n2. {#fruits}{name}{~n}{\/fruits} is the syntax to loop through an array named &#8216;fruits&#8217;.<br \/>\n3. {?name}&#8230;{:else}&#8230;{\/name} is used for conditional if else logic implementation.<br \/>\n4. {name|s|h|u} is used to apply various filters. For example &#8220;s&#8221; is used to disable auto-escaping.<\/p>\n<p>Likewise we have plenty of other tags through which we can use &#8220;dust&#8221; for templating our views effectively.<\/p>\n<p>Hope this will help \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our node js application, we can use variety of templating engines like &#8220;jade&#8221; , &#8220;ejs&#8221;, &#8220;mustache&#8221; etc. Dust.js is an asynchronous templating engine which can be used both on server side as well as client side and provide easy caching with high performance. In my previous blog on EJS as a templating engine, i [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[1347,1129,1124,1191],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12264"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12264"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12264\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}