Skip to content

Instantly share code, notes, and snippets.

@greduan
Created February 3, 2018 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greduan/9f624208912bea7299c97ce0721d9b16 to your computer and use it in GitHub Desktop.
Save greduan/9f624208912bea7299c97ce0721d9b16 to your computer and use it in GitHub Desktop.
A synchronous, minimal render()-generating function for use with Express or Koa (or whatever)
const fs = require('fs')
const path = require('path')
const Handlebars = require('handlebars')
const generateHandlebarsTemplate =
templateSource => Handlebars.compile(templateSource);
module.exports = viewsPath => {
const files = fs.readdirSync(viewsPath)
const templates = {};
files.forEach(file => {
templates[path.basename(file, '.hbs')] =
generateHandlebarsTemplate(
fs.readFileSync(path.join(viewsPath, file), { encoding: 'utf8' })
)
})
return (name, data) => templates[name](data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment