Skip to content

Instantly share code, notes, and snippets.

@dandean
Created September 9, 2012 04:57
Show Gist options
  • Select an option

  • Save dandean/3682663 to your computer and use it in GitHub Desktop.

Select an option

Save dandean/3682663 to your computer and use it in GitHub Desktop.
Denby extended with Mustache rendering support
var Denby = require('denby');
var Mustache = require('mustache');
/**
* new DenbyMustache(options)
* - options (Object): Configuration options.
*
* See [DenbyMustache#options] and [Denby#options] for more information.
**/
function DenbyMustache(options) {
Denby.apply(this, arguments);
this.template = this.options.template;
this.partials = this.options.partials;
if (this.options.hasOwnProperty('template'))
delete this.options.template;
if (this.options.hasOwnProperty('partials'))
delete this.options.partials;
}
DenbyMustache.prototype = Object.create(Denby.prototype, {
/**
* DenbyMustache#options
*
* - template (String): The mustache template for this instance.
* - partials (Object): Hash of mustache partials, keyed on partial name.
**/
options: {
value: { template: '', partials: {} },
configurable: true, writable: true
},
/**
* DenbyMustache#render([model]) -> undefined
* - model (Object): Optional model to use when rendering the view.
*
* If model is not provided, renders using `this.model`. If model has
* a `toJSON` method, the result of that method is what is passed to
* the mustache engine.
**/
render: {
value: function(model) {
// Find the model and convert it to JSON before possible:
model = model || this.model;
if (model.toJSON) model = model.toJSON();
// Render the template with model data:
this.element.innerHTML =
Mustache.render(this.template, model, this.partials);
// Call the parent render method:
Denby.prototype.render.call(this, model);
},
configurable: true, writable: true
}
});
module.exports = DenbyMustache;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment