Permalink
Browse files

add subdir partials

  • Loading branch information...
1 parent 39f4c17 commit 3f42541b2e91641b8cb381048dea589192f44bd5 @mgutz mgutz committed Apr 6, 2013
Showing with 58 additions and 44 deletions.
  1. +1 −0 example/views/partials/sub/comment.html
  2. +1 −1 example/views/veggies.hbs
  3. +48 −42 lib/hbs.js
  4. +2 −1 package.json
  5. +6 −0 tests/handlebarsSpecs.js
@@ -0,0 +1 @@
+<!-- just a comment -->
@@ -6,7 +6,7 @@
</style>
{{/contentFor}}
-
+{{> sub/comment}}
<h1>{{title}}</h1>
<ul>
{{#each veggies}}
View
@@ -1,5 +1,6 @@
var fs = require('fs');
var path = require('path');
+var glob = require('glob');
exports.handlebars = require('handlebars');
/**
@@ -43,7 +44,7 @@ var defaultLayoutTemplate;
/**
* Regex pattern for layout directive. {{!< layout }}
*/
-var layoutPattern = /\{\{\!\<\s+([A-Za-z0-9\._\-\/]+)\s*\}\}/;
+var layoutPattern = /{{!<\s+([A-Za-z0-9\._\-\/]+)\s*}}/;
/**
@@ -55,10 +56,10 @@ var layoutPattern = /\{\{\!\<\s+([A-Za-z0-9\._\-\/]+)\s*\}\}/;
* {{{block "pageStylesheets"}}}
*/
function block(name) {
- var val = (blocks[name] || []).join('\n');
- // clear the block
- blocks[name] = [];
- return val;
+ var val = (blocks[name] || []).join('\n');
+ // clear the block
+ blocks[name] = [];
+ return val;
}
@@ -72,11 +73,11 @@ function block(name) {
* {{/contentFor}}
*/
function contentFor(name, context) {
- var block = blocks[name];
- if (!block) {
- block = blocks[name] = [];
- }
- block.push(context.fn(this));
+ var block = blocks[name];
+ if (!block) {
+ block = blocks[name] = [];
+ }
+ block.push(context.fn(this));
}
/**
@@ -93,12 +94,14 @@ function cacheLayout(layoutFile, useCache, cb) {
var layoutTemplate = cache[layoutFile] ? cache[layoutFile].layoutTemplate : null;
if (layoutTemplate) return cb(null, layoutTemplate);
- fs.readFile(layoutFile, 'utf8', function(err, str) {
+ fs.readFile(layoutFile, 'utf8', function (err, str) {
if (err) return cb(err);
layoutTemplate = exports.handlebars.compile(str);
if (useCache) {
- cache[layoutFile] = {layoutTemplate: layoutTemplate};
+ cache[layoutFile] = {
+ layoutTemplate: layoutTemplate
+ };
}
cb(null, layoutTemplate);
@@ -112,17 +115,19 @@ function cacheLayout(layoutFile, useCache, cb) {
* @param {String} base The views directory.
*/
function cachePartials() {
- var files = fs.readdirSync(partialsDir);
-
- files.forEach(function(file) {
- var filePath = path.join(partialsDir, file);
- var stats = fs.statSync(filePath);
- if (!stats.isFile()) return;
-
- var source = fs.readFileSync(filePath, 'utf8');
- var name = path.basename(file, path.extname(file));
- exports.handlebars.registerPartial(name, source);
- });
+ var files = glob.sync(partialsDir+'/**/*');
+ files.forEach(function (file) {
+ var stats = fs.statSync(file);
+ if (!stats.isFile()) return;
+ var source = fs.readFileSync(file, 'utf8');
+ // partials names are relative to partialsDir
+ var filepath = file.slice(partialsDir.length + 1);
+ var dirname = path.dirname(filepath);
+ dirname = dirname === '.' ? '' : dirname+'/';
+
+ var name = dirname + path.basename(file, path.extname(file));
+ exports.handlebars.registerPartial(name, source);
+ });
}
@@ -138,7 +143,7 @@ function cachePartials() {
* }
*
*/
-exports.express3 = function(options) {
+exports.express3 = function (options) {
_options = options || {};
if (!_options.extname) _options.extname = '.hbs';
@@ -165,7 +170,7 @@ function loadDefaultLayout(useCache, cb) {
if (!_options.defaultLayout) return cb();
if (useCache && defaultLayoutTemplate) return cb(null, defaultLayoutTemplate);
- cacheLayout(_options.defaultLayout, useCache, function(err, template) {
+ cacheLayout(_options.defaultLayout, useCache, function (err, template) {
if (err) return cb(err);
defaultLayoutTemplate = template;
@@ -178,7 +183,7 @@ function loadDefaultLayout(useCache, cb) {
/**
* express 3.x template engine compliance
*/
-var _express3 = function(filename, options, cb) {
+var _express3 = function (filename, options, cb) {
var handlebars = exports.handlebars;
// Force reloading of all partials if cachine not used. Inefficient but there
@@ -204,10 +209,9 @@ var _express3 = function(filename, options, cb) {
var layout = matches[1];
// cacheLayout expects absolute path
- layout = path.resolve(path.join(layoutsDir?layoutsDir:path.dirname(filename), layout));
+ layout = path.resolve(path.join(layoutsDir ? layoutsDir : path.dirname(filename), layout));
cacheLayout(layout, options.cache, cb);
- }
- else {
+ } else {
cb(null, null);
}
}
@@ -218,8 +222,8 @@ var _express3 = function(filename, options, cb) {
*/
function render(template, locals, layoutTemplate, cb) {
var res = template(locals);
- async.done(function(values) {
- Object.keys(values).forEach(function(id) {
+ async.done(function (values) {
+ Object.keys(values).forEach(function (id) {
res = res.replace(id, values[id]);
});
@@ -229,8 +233,8 @@ var _express3 = function(filename, options, cb) {
locals.body = res;
var layoutResult = layoutTemplate(locals);
- async.done(function(values) {
- Object.keys(values).forEach(function(id) {
+ async.done(function (values) {
+ Object.keys(values).forEach(function (id) {
layoutResult = layoutResult.replace(id, values[id]);
});
@@ -254,16 +258,18 @@ var _express3 = function(filename, options, cb) {
return render(template, locals, layoutTemplate, cb);
}
- fs.readFile(filename, 'utf8', function(err, str) {
+ fs.readFile(filename, 'utf8', function (err, str) {
if (err) return cb(err);
var template = handlebars.compile(str);
if (options.cache) {
- cache[filename] = { template: template };
+ cache[filename] = {
+ template: template
+ };
}
// Try to get the layout
- parseLayout(str, filename, function(err, layoutTemplate) {
+ parseLayout(str, filename, function (err, layoutTemplate) {
if (err) return cb(err);
function renderIt(layoutTemplate) {
@@ -280,10 +286,10 @@ var _express3 = function(filename, options, cb) {
}
// 2. Layout specified by options from render
- else if (typeof(options.layout) !== 'undefined') {
+ else if (typeof (options.layout) !== 'undefined') {
if (options.layout) {
var layoutFile = path.resolve(path.join(path.dirname(filename), options.layout));
- cacheLayout(layoutFile, options.cache, function(err, layoutTemplate) {
+ cacheLayout(layoutFile, options.cache, function (err, layoutTemplate) {
if (err) return cb(err);
renderIt(layoutTemplate);
});
@@ -306,7 +312,7 @@ var _express3 = function(filename, options, cb) {
}
// kick it off by loading default template (if any)
- loadDefaultLayout(options.cache, function(err) {
+ loadDefaultLayout(options.cache, function (err) {
if (err) return cb(err);
return compileFile(options, cb);
});
@@ -325,12 +331,12 @@ exports.registerPartial = function () {
exports.handlebars.registerPartial.apply(exports.handlebars, arguments);
};
-exports.registerAsyncHelper = function(name, fn) {
- exports.handlebars.registerHelper(name, function(context) {
+exports.registerAsyncHelper = function (name, fn) {
+ exports.handlebars.registerHelper(name, function (context) {
return async.resolve(fn, context);
});
};
// DEPRECATED, kept for backwards compatibility
exports.SafeString = exports.handlebars.SafeString;
-exports.Utils = exports.handlebars.Utils;
+exports.Utils = exports.handlebars.Utils;
View
@@ -20,6 +20,7 @@
"grunt-simple-mocha": "git://github.com/yaymukund/grunt-simple-mocha.git"
},
"dependencies": {
- "handlebars": "~1.0.7"
+ "handlebars": "~1.0.7",
+ "glob": "~3.1.21"
}
}
View
@@ -30,6 +30,12 @@ describe('express-hbs', function() {
.expect(/jquery\.js/, done);
});
+ it('should render sub partial', function(done) {
+ request(app)
+ .get('/veggies')
+ .expect(/just a comment/, done);
+ });
+
it('should render block', function(done) {
request(app)
.get('/')

0 comments on commit 3f42541

Please sign in to comment.