diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-25 11:58:55 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-25 11:58:55 +0200 |
commit | f6e123f1ed36019a2ec5da1f97b27d22352b689a (patch) | |
tree | 326670b5cf1ff0cbdfbcd35d53f12fb858f874b2 /lib/models/templateEngine.js | |
parent | e1e4e7f01177d968e63f0b3a1830eda1adacb56b (diff) | |
download | gitbook-f6e123f1ed36019a2ec5da1f97b27d22352b689a.zip gitbook-f6e123f1ed36019a2ec5da1f97b27d22352b689a.tar.gz gitbook-f6e123f1ed36019a2ec5da1f97b27d22352b689a.tar.bz2 |
Add base rendering for template
Diffstat (limited to 'lib/models/templateEngine.js')
-rw-r--r-- | lib/models/templateEngine.js | 26 |
1 files changed, 25 insertions, 1 deletions
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; |