Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags parameter to Mustache.render #306

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions mustache.js
Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions test/_files/tags_template.js
@@ -0,0 +1,4 @@
({
first: "It worked the first time.",
second: "And it worked the second time.",
})
3 changes: 3 additions & 0 deletions test/_files/tags_template.mustache
@@ -0,0 +1,3 @@
*
<% first %>
* <% second %>
1 change: 1 addition & 0 deletions test/_files/tags_template.tags
@@ -0,0 +1 @@
["<%", "%>"]
3 changes: 3 additions & 0 deletions test/_files/tags_template.txt
@@ -0,0 +1,3 @@
*
It worked the first time.
* And it worked the second time.
18 changes: 12 additions & 6 deletions test/render-test.js
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
});
Expand Down