diff options
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/plugin.js | 5 | ||||
-rw-r--r-- | lib/models/templateBlock.js | 20 | ||||
-rw-r--r-- | lib/models/templateEngine.js | 26 |
3 files changed, 47 insertions, 4 deletions
diff --git a/lib/models/plugin.js b/lib/models/plugin.js index 6891d7d..ed7ad82 100644 --- a/lib/models/plugin.js +++ b/lib/models/plugin.js @@ -87,12 +87,13 @@ Plugin.prototype.getFilters = function() { */ Plugin.prototype.getBlocks = function() { var blocks = this.getContent().get('blocks'); + blocks = blocks || Immutable.Map(); return blocks .map(function(block, blockName) { - block.name = blockName; - return new TemplateBlock(block); + return TemplateBlock.create(blockName, block); }) + .valueSeq() .toList(); }; diff --git a/lib/models/templateBlock.js b/lib/models/templateBlock.js index ec7dc7c..cce636e 100644 --- a/lib/models/templateBlock.js +++ b/lib/models/templateBlock.js @@ -16,7 +16,7 @@ var TemplateBlock = Immutable.Record({ shortcuts: Immutable.List(), post: null, parse: true -}); +}, 'TemplateBlock'); TemplateBlock.prototype.getName = function() { return this.get('name'); @@ -251,6 +251,24 @@ TemplateBlock.indexBlockResult = function(blk) { }; /** + Create a template block from a function or an object + + @param {String} blockName + @param {Object} block + @return {TemplateBlock} +*/ +TemplateBlock.create = function(blockName, block) { + if (is.fn(block)) { + block = new Immutable.Map({ + process: block + }); + } + + block = block.set('name', blockName); + return new TemplateBlock(block); +}; + +/** Extract kwargs from an arguments array @param {Array} args diff --git a/lib/models/templateEngine.js b/lib/models/templateEngine.js index 1f93879..aaa70fd 100644 --- a/lib/models/templateEngine.js +++ b/lib/models/templateEngine.js @@ -1,6 +1,8 @@ var nunjucks = require('nunjucks'); var Immutable = require('immutable'); +var Promise = require('../utils/promise'); + var TemplateEngine = Immutable.Record({ // List of {TemplateBlock} blocks: Immutable.List(), @@ -16,7 +18,7 @@ var TemplateEngine = Immutable.Record({ // Nunjucks loader loader: nunjucks.FileSystemLoader('views') -}); +}, 'TemplateEngine'); TemplateEngine.prototype.getBlocks = function() { return this.get('blocks'); @@ -103,4 +105,26 @@ TemplateEngine.prototype.bindToContext = function(fn) { return fn.bind(this.getContext()); }; +/** + Render a template + + @param {String} filePath + @param {String} content + @param {Object} ctx + @return {Promise<String>} +*/ +TemplateEngine.prototype.render = function renderTemplate(filePath, content, context) { + context = context || {}; + var env = this.toNunjucks(); + + return Promise.nfcall( + env.renderString.bind(env), + content, + context, + { + path: filePath + } + ); +}; + module.exports = TemplateEngine; |