Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
});
@CharliePops

This comment has been minimized.

Copy link

@CharliePops CharliePops commented Jan 8, 2013

Thanks, very useful.

@gilbertbw

This comment has been minimized.

Copy link

@gilbertbw gilbertbw commented Mar 8, 2013

on line 18 you want to return hbs.registerPartial(name, template); I belive

@ChristianRich

This comment has been minimized.

Copy link

@ChristianRich ChristianRich commented Aug 10, 2016

Highly useful. Thanks.

@oceansize

This comment has been minimized.

Copy link

@oceansize oceansize commented Mar 19, 2017

Brilliant! Thank you for this :)

@tapankumar

This comment has been minimized.

Copy link

@tapankumar tapankumar commented Jul 13, 2017

Thanks.. It works..

If you want to read the directory recursively. Use below code

var dir = path.join(__dirname, 'views');
//console.log(partialsDir);

const walkSync = (dir, filelist = []) => {
  fs.readdirSync(dir).forEach(file => {

    filelist = fs.statSync(path.join(dir, file)).isDirectory()
      ? walkSync(path.join(dir, file), filelist)
      : filelist.concat(path.join(dir, file));

  });
  return filelist;
}

var filelist = walkSync(dir);
if (filelist.length > 0) {
  filelist.forEach(function (filename) {
    var matches = /^([^.]+).hbs$/.exec(path.basename(filename));
    if (!matches) {
      return;
    }
    var name = matches[1];
    console.log(name);
    var template = fs.readFileSync(filename, 'utf8');
    Handlebars.registerPartial(name, template);
  });
}
@mdcovarr

This comment has been minimized.

Copy link

@mdcovarr mdcovarr commented Nov 24, 2017

thanks you very helpful!

@tcelestino

This comment has been minimized.

Copy link

@tcelestino tcelestino commented Oct 9, 2018

thanks a lot about it!! 🦀

@biswajit-saha

This comment has been minimized.

Copy link

@biswajit-saha biswajit-saha commented Jan 19, 2019

@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

This comment has been minimized.

Copy link

@sebastianpikand sebastianpikand commented Mar 4, 2020

@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

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