From 0d8b00fa6716029bad48030af1da2a9267f4e869 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Thu, 27 Dec 2012 13:12:07 -0800 Subject: [PATCH] Allow compiling of tokens without template This commit allows pre-compiled templates to be executed without keeping the original template around. It is still required when using higher-order functions. Fixes #262 --- CHANGES | 7 +++++++ mustache.js | 10 +++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 1a3f250..f8bc28f 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,10 @@ += HEAD + + * Don't require the original template to be passed to the rendering function + when using compiled templates. This is still required when using higher-order + functions in order to be able to extract the portion of the template + that was contained by that section. Fixes #262 + = 0.7.2 / 27 Dec 2012 * Fixed a rendering bug (#274) when using nested higher-order sections. diff --git a/mustache.js b/mustache.js index 932052b..94024ae 100644 --- a/mustache.js +++ b/mustache.js @@ -230,7 +230,8 @@ return this.compile(template)(view, partials); }; - Writer.prototype._section = function (name, context, text, callback) { + Writer.prototype._section = function (token, context, template, callback) { + var name = token[1]; var value = context.lookup(name); switch (typeof value) { @@ -247,6 +248,7 @@ return value ? callback(this, context.push(value)) : ""; case "function": + var text = template == null ? null : template.slice(token[3], token[5]); var self = this; var scopedRender = function (template) { return self.render(template, context); @@ -319,15 +321,13 @@ return function (writer, context, template) { var buffer = ""; - var token, sectionText; + var token; for (var i = 0, len = tokens.length; i < len; ++i) { token = tokens[i]; - switch (token[0]) { case "#": - sectionText = template.slice(token[3], token[5]); - buffer += writer._section(token[1], context, sectionText, subRender(i, token[4], template)); + buffer += writer._section(token, context, template, subRender(i, token[4], template)); break; case "^": buffer += writer._inverted(token[1], context, subRender(i, token[4], template));