Dustjs helpers, latest version of dust.js #93

Merged
merged 2 commits into from Mar 28, 2013

4 participants

@seriousManual

Since version 1.1 helpers are no longer included in the dust core, this pull requests additionally installs them.

@tj tj merged commit c302031 into tj:master Mar 28, 2013
@PaquitoSoft

Is there any way to use custom helpers when using DustJS through Consolidate?
When I register some new custom helpers they are not available in templates.
As far as I can tell, it may be because of consolidate requiring dustjs-helpers internally (consolidate.js:194):

exports.dust.render = function(str, options, fn){
  var engine = requires.dust;
  if (!engine) {
    try {
      engine = requires.dust = require('dust');
    } catch (err) {
      try {
        engine = requires.dust = require('dustjs-helpers');
      } catch (err) {
        engine = requires.dust = require('dustjs-linkedin');
      }
    }
  }

  var ext = 'dust'
    , views = '.';

  if (options) {
    if (options.ext) ext = options.ext;
    if (options.views) views = options.views;
    if (options.settings && options.settings.views) views = options.settings.views;
  }
  if (!options || (options && !options.cache)) engine.cache = {};

  engine.onLoad = function(path, callback){
    if ('' == extname(path)) path += '.' + ext;
    if ('/' !== path[0]) path = views + '/' + path;
    read(path, options, callback);
  };

  try {
    var tmpl = cache(options) || cache(options, engine.compileFn(str));
    tmpl(options, fn);
  } catch (err) {
    fn(err);
  }
};

So if I add any function to dust object in my app code, that dust instance is no used in consolidate.

Maybe I'm wrong and I didn't find the right way of implementing this.

Thanks.

@tiefenb

I'm also very interested in using costum dustjs helpers with consolidate.js.

@tiefenb

I have created an fork with custom dustjs-helper support. It works well:
https://github.com/tiefenb/consolidate.js

usage example:
https://github.com/tiefenb/consolidate.js#custom-dustjs-helpers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment