From 5ea5f62fc032457b91806deaf7f620bdd74a616c Mon Sep 17 00:00:00 2001 From: Matthew Kastor Date: Sat, 20 Oct 2012 17:56:35 -0400 Subject: [PATCH] template.js - cache, settings now instance fields `cache` and `settings` are no longer private fields. This was done so that plugins could use this class without messing up the final output when template name collisions occur. i.e. each instance has it's own template cache. The `settings` were made specific to each instance so that users of this class could redefine the underscore template settings if they want to. --- rhino_modules/jsdoc/template.js | 37 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/rhino_modules/jsdoc/template.js b/rhino_modules/jsdoc/template.js index 31dcded3..de084a0e 100644 --- a/rhino_modules/jsdoc/template.js +++ b/rhino_modules/jsdoc/template.js @@ -1,19 +1,20 @@ /** - @overview - @author Rafał Wrzeszcz - @license Apache License 2.0 - See file 'LICENSE.md' in this project. + * @file Wrapper for underscore's template utility to allow loading templates from files. + * @author Rafał Wrzeszcz + * @author Matthew Christopher Kastor-Inare III + * @license Apache License 2.0 - See file 'LICENSE.md' in this project. + * @change 2012-10-20 cache and settings are no longer private fields. + * This was done so that plugins could use this class without messing up + * the final output when template name collisions occur. i.e. each instance + * has it's own template cache. The settings were made specific to each + * instance so that users of this class could redefine the underscore + * template settings if they want to. */ var _ = require('underscore'), fs = require('fs'), path = require('path'); -// override default settings -var settings = { - evaluate: /<\?js([\s\S]+?)\?>/g, - interpolate: /<\?js=([\s\S]+?)\?>/g, - escape: /<\?js~([\s\S]+?)\?>/g -}; /** @module jsdoc/template @@ -27,6 +28,14 @@ var settings = { exports.Template = function(path) { this.path = path; this.layout = null; + this.cache = {}; + // override default template tag settings + this.settings = { + evaluate : /<\?js([\s\S]+?)\?>/g, + interpolate: /<\?js=([\s\S]+?)\?>/g, + escape : /<\?js~([\s\S]+?)\?>/g + }; + }; /** Loads template from given file. @@ -35,11 +44,9 @@ exports.Template = function(path) { */ exports.Template.prototype.load = function(file) { var _path = path.join(this.path, file); - return _.template(fs.readFileSync(_path), null, settings); + return _.template(fs.readFileSync(_path), null, this.settings); }; -// templates cache -var cache = {}; /** Renders template using given data. @@ -52,12 +59,12 @@ var cache = {}; */ exports.Template.prototype.partial = function(file, data) { // load template into cache - if (!(file in cache)) { - cache[file] = this.load(file); + if (!(file in this.cache)) { + this.cache[file] = this.load(file); } // keep template helper context - return cache[file].call(this, data); + return this.cache[file].call(this, data); }; /**