Skip to content

Instantly share code, notes, and snippets.

@dhollenbeck
Created March 31, 2016 12:38
Show Gist options
  • Save dhollenbeck/5d2c94e8e2c6dff9d1503ce2f0ab3593 to your computer and use it in GitHub Desktop.
Save dhollenbeck/5d2c94e8e2c6dff9d1503ce2f0ab3593 to your computer and use it in GitHub Desktop.
Node.js express-handlebars YAML Front Matter (YFM) example
'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