Skip to content

Commit efdeb55

Browse files
raymond-lamphillipj
authored andcommitted
Add tags parameter to .render()
1 parent 8e45409 commit efdeb55

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ Following is an [rtype](https://git.io/rtype) signature of the most commonly use
117117

118118
```js
119119
Mustache.render(
120-
template : String,
121-
view : Object,
122-
partials? : Object,
120+
template : String,
121+
view : Object,
122+
partials? : Object,
123+
tags = ['{{', '}}'] : Tags,
123124
) => String
124125

125126
Mustache.parse(
@@ -181,7 +182,7 @@ function loadUser() {
181182
182183
The most basic tag type is a simple variable. A `{{name}}` tag renders the value of the `name` key in the current context. If there is no such key, nothing is rendered.
183184
184-
All variables are HTML-escaped by default. If you want to render unescaped HTML, use the triple mustache: `{{{name}}}`. You can also use `&` to unescape a variable.
185+
All variables are HTML-escaped by default. If you want to render unescaped HTML, use the triple mustache: `{{{name}}}`. You can also use `&` to unescape a variable.
185186
186187
If you'd like to change HTML-escaping behavior globally (for example, to template non-HTML formats), you can override Mustache's escape function. For example, to disable all escaping: `Mustache.escape = function(text) {return text;};`.
187188
@@ -504,12 +505,17 @@ Custom delimiters can be used in place of `{{` and `}}` by setting the new value
504505
505506
#### Setting in JavaScript
506507
507-
The `Mustache.tags` property holds an array consisting of the opening and closing tag values. Set custom values by setting this property.
508+
The `Mustache.tags` property holds an array consisting of the opening and closing tag values. Set custom values by passing a new array of tags to `render()`, which gets honored over the default values, or by overriding the `Mustache.tags` property itself:
508509
509510
```js
510511
var customTags = [ '<%', '%>' ];
511512
```
512513
514+
##### Pass Value into Render Method
515+
```js
516+
Mustache.render(template, view, {}, customTags);
517+
```
518+
513519
##### Override Tags Property
514520
```js
515521
Mustache.tags = customTags;

mustache.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,13 @@
462462
* names and templates of partials that are used in the template. It may
463463
* also be a function that is used to load partial templates on the fly
464464
* that takes a single argument: the name of the partial.
465+
*
466+
* If the optional `tags` argument is given here it must be an array with two
467+
* string values: the opening and closing tags used in the template (e.g.
468+
* [ "<%", "%>" ]). The default is to mustache.tags.
465469
*/
466-
Writer.prototype.render = function render (template, view, partials) {
467-
var tokens = this.parse(template);
470+
Writer.prototype.render = function render (template, view, partials, tags) {
471+
var tokens = this.parse(template, tags);
468472
var context = (view instanceof Context) ? view : new Context(view);
469473
return this.renderTokens(tokens, context, partials, template);
470474
};
@@ -593,16 +597,18 @@
593597

594598
/**
595599
* Renders the `template` with the given `view` and `partials` using the
596-
* default writer.
600+
* default writer. If the optional `tags` argument is given here it must be an
601+
* array with two string values: the opening and closing tags used in the
602+
* template (e.g. [ "<%", "%>" ]). The default is to mustache.tags.
597603
*/
598-
mustache.render = function render (template, view, partials) {
604+
mustache.render = function render (template, view, partials, tags) {
599605
if (typeof template !== 'string') {
600606
throw new TypeError('Invalid template! Template should be a "string" ' +
601607
'but "' + typeStr(template) + '" was given as the first ' +
602608
'argument for mustache#render(template, view, partials)');
603609
}
604610

605-
return defaultWriter.render(template, view, partials);
611+
return defaultWriter.render(template, view, partials, tags);
606612
};
607613

608614
// This is here for backwards compatibility with 0.4.x.,

test/render-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,38 @@ describe('Mustache.render', function () {
1717
'for mustache#render(template, view, partials)');
1818
});
1919

20+
it('uses tags argument instead of Mustache.tags when given', function () {
21+
var template = '<<placeholder>>bar{{placeholder}}';
22+
23+
Mustache.tags = ['{{', '}}'];
24+
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']), 'foobar{{placeholder}}');
25+
});
26+
27+
it('uses tags argument instead of Mustache.tags when given, even when it previous rendered the template using Mustache.tags', function () {
28+
var template = '((placeholder))bar{{placeholder}}';
29+
30+
Mustache.tags = ['{{', '}}'];
31+
Mustache.render(template, { placeholder: 'foo' });
32+
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['((', '))']), 'foobar{{placeholder}}');
33+
});
34+
35+
it('uses tags argument instead of Mustache.tags when given, even when it previous rendered the template using different tags', function () {
36+
var template = '[[placeholder]]bar<<placeholder>>';
37+
38+
Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']);
39+
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['[[', ']]']), 'foobar<<placeholder>>');
40+
});
41+
42+
it('does not mutate Mustache.tags when given tags argument', function() {
43+
var correctMustacheTags = ['{{', '}}'];
44+
Mustache.tags = correctMustacheTags;
45+
46+
Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, ['((', '))']);
47+
48+
assert.equal(Mustache.tags, correctMustacheTags);
49+
assert.deepEqual(Mustache.tags, ['{{', '}}']);
50+
});
51+
2052
tests.forEach(function (test) {
2153
var view = eval(test.view);
2254

0 commit comments

Comments
 (0)