Search code examples
expressdust.js

Loading partials with dust express


How can I load a partial view with dust server-side rendering. I have tried

{>"../partials/head"/}

Which just gets removed from the rendered output.

the view folder structure is like

views
    pages
        main.dust
    partials
        head.dust

I am using the following package https://github.com/krakenjs/adaro


Solution

  • Dust doesn't understand filesystem layout-- it is just a string renderer.

    If you want Dust to try to load templates from other locations, you should write a loader to help. You attach this loader to the hook dust.onLoad.

    A loader looks like this:

    dust.onLoad = function(templateName, callback) {
      // do some path calculation maybe
      fs.readFile(templateName + '.js', { encoding: 'utf8' }, function(err, data) {
        callback(err, data); // node-style callback
      });
    };
    

    When you invoke a partial like {> "../partials/head" /}, your function will be invoked with ../partials/head as the first argument. You can use path and fs methods to load the correct file and pass it to the callback.

    More information: http://www.dustjs.com/guides/onload/