Created
January 28, 2014 09:31
-
-
Save AdoPi/8664633 to your computer and use it in GitHub Desktop.
handlebar helpers for express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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