How to use the dustjs-linkedin.onLoad function in dustjs-linkedin

To help you get started, we’ve selected a few dustjs-linkedin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github linkedin / dustjs / examples / streaming / app.js View on Github external
var fs = require('fs'),
    path = require('path'),
    express = require('express'),
    request = require('request'),
    dust = require('dustjs-linkedin');

dust.config.whitespace = true;
dust.config.cache = false;

// Define a custom `onLoad` function to tell Dust how to load templates
dust.onLoad = function(tmpl, cb) {
  fs.readFile(path.join('./views', path.relative('/', path.resolve('/', tmpl + '.dust'))),
              { encoding: 'utf8' }, cb);
};

var app = express();
app.get('/', function (req, res) {
  dust.stream("hello", {
    "async": request('http://www.dustjs.com/')
  }).pipe(res);
});

app.listen(3000, function () {
  console.log('Visit http://localhost:3000 to see streaming!');
});
github linkedin / dustjs / examples / streaming-incremental / app.js View on Github external
var fs = require('fs'),
    path = require('path'),
    express = require('express'),
    dust = require('dustjs-linkedin'),
    q = require('q');

// Define a custom `onLoad` function to tell Dust how to load templates
dust.onLoad = function(tmpl, cb) {
  fs.readFile(path.join('./views', path.relative('/', path.resolve('/', tmpl + '.dust'))),
              { encoding: 'utf8' }, cb);
};

var app = express();

var position;
var context = {
  wait: function(chunk, context, bodies, params) {
    var delayMilliseconds = parseInt(params.delay, 10) * 1000;
    // Returning a Promise-- Dust will wait for the promise to resolve
    var promise = q(position++).delay(delayMilliseconds);
    promise.then(function(position) {
      console.log('Rendering', params.name, 'which started in position', position);
    });
    return promise;
github gitana / cloudcms-server / duster / index.js View on Github external
*/
dust.isThenable = function(elem) {
    return elem &&
        typeof elem === 'object' &&
        typeof elem.then === 'function' && !elem.objectType;
};

/**
 * Override Dust's onLoad() function so that templates are loaded from the store.
 * The cache key is also determined to include the appId.
 *
 * @param templatePath
 * @param options
 * @param callback
 */
var loadTemplate = dust.onLoad = function(templatePath, options, callback)
{
    //var log = options.log;

    // `templateName` is the name of the template requested by dust.render / dust.stream
    // or via a partial include, like {> "hello-world" /}
    // `options` can be set as part of a Context. They will be explored later
    // `callback` is a Node-like callback that takes (err, data)

    var store = options.store;

    store.existsFile(templatePath, function(exists) {

        if (!exists) {
            return callback({
                "message": "Dust cannot find file: " + templatePath
            });
github getlackey / lackey-cms / modules / core / client / js / template.js View on Github external
}
};

function load(name) {
    return xhr.basedGet('dust/' + name + '.js')
        .then(template => {
            // need to do that so we don't have to expose dust compile
            /*jslint evil: true */
            let loadTemplate = new Function('dust', template); //eslint-disable-line no-new-func
            /*jslint evil: false */
            loadTemplate(engine);
            return template;
        });
}

engine.onLoad = function () {


    let templateName = arguments[0],
        callback;
    if (arguments.length > 2) {
        /* istanbul ignore next */
        callback = arguments[2];
    } else {
        callback = arguments[1];
    }
    return load(templateName).then((template) => {
        if (!template) {
            return callback(new Error('Template ' + templateName + ' not found'));
        }
        callback(null, template);
    }, (error) => {
github Postleaf / postleaf / source / modules / dust_engine.js View on Github external
}
    }

    // Found, return the successful candidate
    return callback(null, candidate);
  });
}

// Disable Dust.js caching so we can handle it ourselves
Dust.config.cache = false;

// Enable whitespace for more readable outputs
Dust.config.whitespace = true;

// Locate the requested template
Dust.onLoad = (name, options, callback) => {
  let file;

  // Absolute path
  if(Path.isAbsolute(name)) {
    file = name;

    // Try cache first
    if(templateCache[file]) {
      try {
        return callback(null, Dust.loadSource(templateCache[file]));
      } catch(err) {
        return callback(err);
      }
    }

    // Load and compile template
github dadi / web / dadi / lib / dust / index.js View on Github external
const Dust = function () {
  this.templates = {}

  // Loading core Dust helpers
  require('dustjs-helpers')

  dust.onLoad = (templateName, opts, callback) => {
    var name
    if (opts && opts.host && templateName.indexOf(opts.host) === -1 && this.templates[opts.host + templateName]) {
      name = opts.host + templateName
    } else {
      name = templateName
    }

    if (!this.templates[name]) {
      return callback({message: 'Template not found: ' + name}, null)
    }

    var compiled = dust.compile(this.templates[name])
    var tmpl = dust.loadSource(compiled)

    return callback(null, tmpl)
  }
github linkedin / dustjs / examples / commonjs / requireTemplate.js View on Github external
function requireTemplate(name, callback) {
  var tmpl;
  try {
    tmpl = require(root(name))(dust);
  } catch(e) {
    if (callback) { callback(e); } else { throw e; }
  }
  if (callback) {
    callback(null, tmpl);
  } else {
    return tmpl;
  }
}

// Adding an `onLoad` function lets us load partials
module.exports = dust.onLoad = requireTemplate;
github marko-js / marko / test / dust-tests.js View on Github external
'use strict';
var chai = require('chai');
chai.Assertion.includeStack = true;
require('chai').should();
var expect = require('chai').expect;
var nodePath = require('path');
var fs = require('fs');

var dust = require('dustjs-linkedin');
dust.onLoad = function(path, callback) {
    if (!fs.existsSync(path)) {
        if (!path.endsWith('.dust')) {
            path += '.dust';
        }
    }

    fs.readFile(path, 'utf-8', callback);
};


require('marko-async/dust').registerHelpers(dust);

function testRender(path, data, done, options) {
    options = options || {};

    var inputPath = nodePath.join(__dirname, path);
github krakenjs / adaro / test / reader.js View on Github external
after(function () {
            dustjs.onLoad = undefined;
        });
github lasso-js / lasso / test / dust-tests.js View on Github external
beforeEach(function(done) {
        var dust = require('dustjs-linkedin');
        dust.onLoad = function(path, callback) {
            if (!fs.existsSync(path)) {
                if (!path.endsWith('.dust')) {
                    path += '.dust';
                }
            }

            fs.readFile(path, 'utf-8', callback);
        };


        require('../dust').registerHelpers(dust);

        require('../').configure({
            fileWriter: {
                outputDir: nodePath.join(__dirname, 'build'),
                urlPrefix: '/static',