Skip to content

Commit

Permalink
fix handlebars helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Filirom1 committed May 21, 2012
1 parent eff65ed commit 2d20335
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
12 changes: 10 additions & 2 deletions example/handlebars/helpers/fullName.js
@@ -1,3 +1,11 @@
// Only export one function containing all the code.
// The code will be copied in template.js via require('./helpers/fullName').toString()
module.exports = function(person) {
return person.firstName + " " + person.lastName;
}
return person.firstName + " " + person.lastName + " " + foo();

function foo(){
return 'foo';
}
};

// Nothing outside module.exports
14 changes: 13 additions & 1 deletion example/handlebars/index.html
Expand Up @@ -9,13 +9,25 @@
</head>
<body>
<div id="content"></div>
<br />
<div id="helpers"></div>
<script>
var data = {
title: 'foobar'
};

var tmp = JST.sample( data );
var tmp = JST.partials( data );
var content = $('#content').html( tmp );


var content = $('#helpers').html( JST.helpers({
author: {firstName: "Alan", lastName: "Johnson"},
body: "I Love Handlebars",
comments: [{
author: {firstName: "Yehuda", lastName: "Katz"},
body: "Me too!"
}]
}) );
</script>
</body>
</html>
10 changes: 4 additions & 6 deletions lib/engines/handlebars.js
Expand Up @@ -6,7 +6,7 @@ var jst = require('../index.js'),

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

// `options.helpers` directory containing helper files.
// See `example/handlebars/helpers`
Expand All @@ -18,7 +18,7 @@ module.exports = function( options, nm, file_contents ){
if( options.verbose ) { console.log('Register helper ' + file); }
var helper = helpers[file] = require(Path.resolve(Path.join(options.helpers, file)) );

handlebars.registerHelper(file, helper);
output.push('Handlebars.registerHelper("' + file + '", ' + helper.toString() + ');');
});
}

Expand All @@ -27,10 +27,8 @@ module.exports = function( options, nm, file_contents ){
// delete any cached versions of the template
compiled_hbs = handlebars.precompile( file_contents );

output = [
options.namespace + '["'+ nm +'"] = Handlebars.template('+ compiled_hbs +');\n',
'Handlebars.partials["'+ nm.replace(/\//g, '.') +'"] = ' + options.namespace + '["'+ nm +'"]'
];
output.push(options.namespace + '["'+ nm +'"] = Handlebars.template('+ compiled_hbs +');\n');
output.push('Handlebars.partials["'+ nm.replace(/\//g, '.') +'"] = ' + options.namespace + '["'+ nm +'"]');

} catch( e ){
console.error( 'Error processing'+ nm, e);
Expand Down
2 changes: 1 addition & 1 deletion test/universalJstTest.js
Expand Up @@ -98,7 +98,7 @@ vows.describe('Test universal JST').addBatch({
body: "Me too!"
}]
};
assert.include(window.JST.helpers(context), '<h1>By Alan Johnson</h1>');
assert.include(window.JST.helpers(context), '<h1>By Alan Johnson');
assert.include(window.JST.partials({ title: 'hello'}), '<h1>hello</h1>');
assert.include(window.JST.partials({ title: 'hello'}), 'This is a plain template');
assert.include(window.JST.partials({ title: 'hello'}), 'This is a partial in a subdirectory');
Expand Down

0 comments on commit 2d20335

Please sign in to comment.