Skip to content

Instantly share code, notes, and snippets.

@AdoPi
Created January 28, 2014 09:31
Show Gist options
  • Save AdoPi/8664633 to your computer and use it in GitHub Desktop.
Save AdoPi/8664633 to your computer and use it in GitHub Desktop.
handlebar helpers for express
//handlebarjs
var hbs = require('express3-handlebars').create({
helpers: {
img: function(name,alt) { return '<img src="images/'+name+'" alt="'+alt+'">';},
styles: function() {
//get directory name
var fs = require('fs');
var files;
/* make the array which will be used for css files*/
files= fs.readdirSync(__dirname+'/public/stylesheets');
//make the string result
var result='';
for (var i = files.length - 1; i >= 0; i--) {
var file = files[i];
var tab = file.split('.');
if (tab[tab.length-1]=='css')
result+='<link rel="stylesheet" href="stylesheets/'+file+'">';
}
return result;
},
css: function(name) {
return '<link rel="stylesheet" href="stylesheets/'+name+'.css">';
},
js: function(name) {
return '<script src="javascripts/'+name+'.js" type="text/javascript"></script>';
}
}
});
app.engine('handlebars', hbs.engine);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment