Skip to content

Instantly share code, notes, and snippets.

@benw
Created October 3, 2012 00:29
  • Star 36 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save benw/3824204 to your computer and use it in GitHub Desktop.
Loads partial handlebars templates from files in a directory
// Helps with this problem:
// http://stackoverflow.com/questions/8059914/express-js-hbs-module-register-partials-from-hbs-file
var hbs = require('hbs');
var fs = require('fs');
var partialsDir = __dirname + '/../views/partials';
var filenames = fs.readdirSync(partialsDir);
filenames.forEach(function (filename) {
var matches = /^([^.]+).hbs$/.exec(filename);
if (!matches) {
return;
}
var name = matches[1];
var template = fs.readFileSync(partialsDir + '/' + filename, 'utf8');
hbs.registerPartial(name, template);
});
@mdcovarr
Copy link

thanks you very helpful!

@tcelestino
Copy link

thanks a lot about it!! 🦀

@biswajit-saha
Copy link

@tapankumar thank you very much for the code. I have a question.
let's say our template structure is like below

Index.hbs
----partials
--------partial1.hbs
--------subfolder
------------partials-under-subfolder.hbs

Now how should I include the partials-under-subfolder within index.hbs template file?

@sebastianpikand
Copy link

@tapankumar thank you very much for the code. I have a question.
let's say our template structure is like below

Index.hbs
----partials
--------partial1.hbs
--------subfolder
------------partials-under-subfolder.hbs

Now how should I include the partials-under-subfolder within index.hbs template file?

had the same problem. seems like {{> subfolder_name/partial_under_subfolder.hbs}} should work

@Heilemann
Copy link

Heilemann commented Apr 19, 2022

nm.

@benw
Copy link
Author

benw commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment