UNPKG

bloggify-ajs-renderer

Version:
68 lines (61 loc) 1.58 kB
"use strict"; var readFile = require("read-file-cache"), noop = require("noop6"), ajs = require("ajs"), typpy = require("typpy"); var disableCache = false; var bloggify = null; /*! * init * * @name init * @function * @param {Object} config The configuration object: * * - `disableCache` (Boolean): If `true`, the cache will be disabled. * * @param {Bloggify} bloggify The `Bloggify` instance. */ exports.init = function (config, _bloggify) { bloggify = _bloggify; disableCache = config.disableCache; bloggify.viewer.registerRenderer("ajs", exports.render); }; /*! * factory * Returns a HTTP request handler. * * @name factory * @function * @param {Function} cb The callback function. * @returns {Function} The request handler. */ exports.factory = function (cb) { return function (ctx) { return cb(function (path, data, cb) { return exports.render(ctx, path, data, cb); }, ctx); }; }; /*! * render * Renders the file. * * @name render * @function * @param {ctx} ctx The context. * @param {String} path The file path. * @param {Object} data The template data. * @param {Function} cb The callback function. */ exports.render = function (ctx, tmpl, data, cb) { cb = cb || noop; ajs.renderFile(tmpl.path, data, { cache: !disableCache }, function (err, html) { if (err) { return cb(err); } data.statusCode = data.statusCode || data.error && data.error.statusCode || 200; ctx.end(html, data.statusCode); cb(null, html); }); };