Created
March 31, 2016 12:38
-
-
Save dhollenbeck/5d2c94e8e2c6dff9d1503ce2f0ab3593 to your computer and use it in GitHub Desktop.
Node.js express-handlebars YAML Front Matter (YFM) example
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
'use strict'; | |
var yfm = require('yfm'); | |
var _ = require('underscore'); | |
var exphbs = require('express-handlebars'); | |
var hbs = exphbs.create({/* options */}); | |
hbs._compileTemplate = function (template, options) { | |
var results = yfm(template); //parse yfm | |
var compiled = this.handlebars.compile(results.content, options); //compile without yfm | |
compiled.yfm = results.context; | |
return compiled; | |
}; | |
hbs._precompileTemplate = function (template, options) { | |
var results = yfm(template); //parse yfm | |
var compiled = this.handlebars.precompile(results.content, options); //compile without yfm | |
compiled.yfm = results.context; | |
return compiled; | |
}; | |
hbs._renderTemplate = function (template, context, options) { | |
// merge yaml front matter into context | |
context = _.extend(context, template.yfm); | |
return template(context, options); | |
}; | |
exports.handlebars = hbs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment