Skip to content

Commit

Permalink
add handlebars helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Filirom1 committed Apr 17, 2012
1 parent 1dcdd4c commit d1eda87
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
3 changes: 3 additions & 0 deletions example/handlebars/helpers/fullName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(person) {
return person.firstName + " " + person.lastName;
}
11 changes: 11 additions & 0 deletions example/handlebars/templates/helpers.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="post">
<h1>By {{fullName author}}</h1>
<div class="body">{{body}}</div>

<h1>Comments</h1>

{{#each comments}}
<h2>By {{fullName author}}</h2>
<div class="body">{{body}}</h2>
{{/each}}
</div>
21 changes: 19 additions & 2 deletions lib/engines/handlebars.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
var jst = require('../index.js'),
handlebars = require('handlebars');
fs = require('fs'),
handlebars = require('handlebars'),
Path = require('path'),
helpers = {};

// Returns a string of js with the compiled template
module.exports = function( options, nm, file_contents ){
var output, compiled_hbs;

// `options.helpers` directory containing helper files.
// See `example/handlebars/helpers`
if(options.helpers && !Object.keys(helpers).length){
var files = fs.readdirSync(options.helpers)
files.forEach(function(file){
if(!/\.js$/.test(file)) return;
file = file.replace(/\.js$/, '');
if( options.verbose ) { console.log('Register helper ' + file); }
var helper = helpers[file] = require(Path.join(options.helpers, file) );

handlebars.registerHelper(file, helper);
});
}

try {
// delete any cached versions of the template
compiled_hbs = handlebars.precompile( file_contents );
Expand All @@ -20,7 +37,7 @@ module.exports = function( options, nm, file_contents ){
];

} catch( e ){
console.log( 'Error processing'+ nm, e);
console.error( 'Error processing'+ nm, e);
return '/* Unable to compile ' + nm + ' */\n';
}

Expand Down
17 changes: 15 additions & 2 deletions test/universalJstTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ vows.describe('Test universal JST').addBatch({
},
'When compiling handlebars JST': {
topic: function(){
engines.handlebars(example('handlebars'), this.callback)
engines.handlebars(example('handlebars'), {
verbose: true,
helpers: join(__dirname, '..', 'example', 'handlebars', 'helpers')
}, this.callback)
},
'Then an array is returned': function(arr){
assert.equal(arr.length, 8);
assert.equal(arr.length, 9);
},
'Then the templates are valid': function(arr){
var str = arr.join('\n');
Expand All @@ -87,6 +90,16 @@ vows.describe('Test universal JST').addBatch({
assert.include(window.JST.multiple_footer({ title: 'hello'}), '<h1>hello</h1>');
assert.include(window.JST.multiple_foo_bar({ foo: [1,2,3] }), '<div>1</div>');
assert.include(window.JST["subfolder/subsub/sample"]({ title: 'hello'}), '<h1>hello</h1>');

var context = {
author: {firstName: "Alan", lastName: "Johnson"},
body: "I Love Handlebars",
comments: [{
author: {firstName: "Yehuda", lastName: "Katz"},
body: "Me too!"
}]
};
assert.include(window.JST.helpers(context), '<h1>By Alan Johnson</h1>');
}
},
'when compiling jquery tmpl jst': {
Expand Down

0 comments on commit d1eda87

Please sign in to comment.