diff --git a/mustache.js b/mustache.js index af48fd1c5..222f007e8 100644 --- a/mustache.js +++ b/mustache.js @@ -225,8 +225,8 @@ }; }; - Writer.prototype.render = function (template, view, partials) { - return this.compile(template)(view, partials); + Writer.prototype.render = function (template, view, partials, tags) { + return this.compile(template, tags)(view, partials); }; /** @@ -446,7 +446,9 @@ } // Match the closing tag. - if (!scanner.scan(tagRes[1])) throw new Error('Unclosed tag at ' + scanner.pos); + if (!scanner.scan(tagRes[1])) { + throw new Error('Unclosed tag at ' + scanner.pos); + } token = [type, value, start, scanner.pos]; tokens.push(token); @@ -512,11 +514,11 @@ }; /** - * Renders the `template` with the given `view` and `partials` using the - * default writer. + * Renders the `template` with the given `view`, `partials`, and `tags` + * using the default writer. */ - exports.render = function (template, view, partials) { - return _writer.render(template, view, partials); + exports.render = function (template, view, partials, tags) { + return _writer.render(template, view, partials, tags); }; // This is here for backwards compatibility with 0.4.x. diff --git a/test/_files/tags_template.js b/test/_files/tags_template.js new file mode 100644 index 000000000..e3c284ce8 --- /dev/null +++ b/test/_files/tags_template.js @@ -0,0 +1,4 @@ +({ + first: "It worked the first time.", + second: "And it worked the second time.", +}) diff --git a/test/_files/tags_template.mustache b/test/_files/tags_template.mustache new file mode 100644 index 000000000..60cd1662c --- /dev/null +++ b/test/_files/tags_template.mustache @@ -0,0 +1,3 @@ +* +<% first %> +* <% second %> diff --git a/test/_files/tags_template.tags b/test/_files/tags_template.tags new file mode 100644 index 000000000..ffd4a0a9f --- /dev/null +++ b/test/_files/tags_template.tags @@ -0,0 +1 @@ +["<%", "%>"] diff --git a/test/_files/tags_template.txt b/test/_files/tags_template.txt new file mode 100644 index 000000000..21cf6caa2 --- /dev/null +++ b/test/_files/tags_template.txt @@ -0,0 +1,3 @@ +* +It worked the first time. +* And it worked the second time. diff --git a/test/render-test.js b/test/render-test.js index acec47ffd..359929e62 100644 --- a/test/render-test.js +++ b/test/render-test.js @@ -22,11 +22,21 @@ function getPartial(testName) { } } +function getTags(testName) { + try { + var tags = getContents(testName, 'tags'); + if (tags) return eval(tags); + } catch (e) { + // No big deal. Not all tests need to test tags support. + } +} + function getTest(testName) { var test = {}; test.view = getView(testName); test.template = getContents(testName, 'mustache'); test.partial = getPartial(testName); + test.tags = getTags(testName); test.expect = getContents(testName, 'txt'); return test; } @@ -55,12 +65,8 @@ describe('Mustache.render', function () { var test = getTest(testName); it('knows how to render ' + testName, function () { - var output; - if (test.partial) { - output = Mustache.render(test.template, test.view, { partial: test.partial }); - } else { - output = Mustache.render(test.template, test.view); - } + var partial = test.partial ? { partial: test.partial } : undefined; + var output = Mustache.render(test.template, test.view, partial, test.tags); assert.equal(output, test.expect); });