Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #65 from ffissore/master

added support to dust partials
  • Loading branch information...
commit dbfdd2afcdfd75ba331df28552002d527abb7fc7 2 parents 97a3235 + f57ba8e
@tj authored
View
18 lib/consolidate.js
@@ -1,4 +1,3 @@
-
/*!
* consolidate
* Copyright(c) 2012 TJ Holowaychuk <tj@vision-media.ca>
@@ -18,7 +17,8 @@
* Module dependencies.
*/
-var fs = require('fs');
+var fs = require('fs'),
+ extname = require('path').extname;
var readCache = {};
@@ -135,6 +135,20 @@ exports.dust.render = function(str, options, fn){
}
}
+ 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;
+ }
+ 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);
View
1  test/consolidate.js
@@ -11,6 +11,7 @@ require('./shared').test('whiskers');
require('./shared').test('haml-coffee');
require('./shared').test('hogan');
require('./shared').test('dust');
+require('./shared/partials').test('dust');
require('./shared').test('handlebars');
require('./shared').test('underscore');
require('./shared').test('qejs');
View
1  test/fixtures/dust/_partial.dust
@@ -0,0 +1 @@
+<p>{user.name} from partial!</p>
View
2  test/fixtures/dust/user_partial.dust
@@ -0,0 +1,2 @@
+{>_partial/}
+<p>{user.name}</p>
View
29 test/shared/partials.js
@@ -0,0 +1,29 @@
+
+var cons = require('../../')
+ , fs = require('fs')
+ , readFile = fs.readFile
+ , readFileSync = fs.readFileSync;
+
+exports.test = function(name) {
+ var user = { name: 'Tobi' };
+
+ describe(name, function(){
+ afterEach(function(){
+ fs.readFile = readFile;
+ fs.readFileSync = readFileSync;
+ });
+
+ it('should support rendering a partial', function(done){
+ var str = fs.readFileSync('test/fixtures/' + name + '/user_partial.' + name).toString();
+ var locals = {
+ user: user,
+ views: "./test/fixtures/" + name
@tj Owner
tj added a note

actually for partials do they need to be relative to the views dir? otherwise we can use the filename's dirname passed so they're relative to the one you rendered, or is that not a dust-friendly sort of thing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ };
+ cons[name].render(str, locals, function(err, html){
+ if (err) return done(err);
+ html.should.equal('<p>Tobi from partial!</p><p>Tobi</p>');
+ done();
+ });
+ });
+ });
+};
@tj

actually for partials do they need to be relative to the views dir? otherwise we can use the filename's dirname passed so they're relative to the one you rendered, or is that not a dust-friendly sort of thing?

Please sign in to comment.
Something went wrong with that request. Please try again.