Created
February 3, 2018 17:17
-
-
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)
This file contains hidden or 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
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