| @@ -117,9 +117,10 @@ Following is an [rtype](https://git.io/rtype) signature of the most commonly use | |||||
| ```js | ```js | ||||
| Mustache.render( | Mustache.render( | ||||
| template : String, | |||||
| view : Object, | |||||
| partials? : Object, | |||||
| template : String, | |||||
| view : Object, | |||||
| partials? : Object, | |||||
| tags = ['{{', '}}'] : Tags, | |||||
| ) => String | ) => String | ||||
| Mustache.parse( | Mustache.parse( | ||||
| @@ -181,7 +182,7 @@ function loadUser() { | |||||
| 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. | 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. | ||||
| 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. | |||||
| 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. | |||||
| 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;};`. | 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;};`. | ||||
| @@ -504,12 +505,17 @@ Custom delimiters can be used in place of `{{` and `}}` by setting the new value | |||||
| #### Setting in JavaScript | #### Setting in JavaScript | ||||
| The `Mustache.tags` property holds an array consisting of the opening and closing tag values. Set custom values by setting this property. | |||||
| 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: | |||||
| ```js | ```js | ||||
| var customTags = [ '<%', '%>' ]; | var customTags = [ '<%', '%>' ]; | ||||
| ``` | ``` | ||||
| ##### Pass Value into Render Method | |||||
| ```js | |||||
| Mustache.render(template, view, {}, customTags); | |||||
| ``` | |||||
| ##### Override Tags Property | ##### Override Tags Property | ||||
| ```js | ```js | ||||
| Mustache.tags = customTags; | Mustache.tags = customTags; | ||||
| @@ -462,9 +462,13 @@ | |||||
| * names and templates of partials that are used in the template. It may | * names and templates of partials that are used in the template. It may | ||||
| * also be a function that is used to load partial templates on the fly | * also be a function that is used to load partial templates on the fly | ||||
| * that takes a single argument: the name of the partial. | * that takes a single argument: the name of the partial. | ||||
| * | |||||
| * If the optional `tags` argument is given here it must be an array with two | |||||
| * string values: the opening and closing tags used in the template (e.g. | |||||
| * [ "<%", "%>" ]). The default is to mustache.tags. | |||||
| */ | */ | ||||
| Writer.prototype.render = function render (template, view, partials) { | |||||
| var tokens = this.parse(template); | |||||
| Writer.prototype.render = function render (template, view, partials, tags) { | |||||
| var tokens = this.parse(template, tags); | |||||
| var context = (view instanceof Context) ? view : new Context(view); | var context = (view instanceof Context) ? view : new Context(view); | ||||
| return this.renderTokens(tokens, context, partials, template); | return this.renderTokens(tokens, context, partials, template); | ||||
| }; | }; | ||||
| @@ -593,16 +597,18 @@ | |||||
| /** | /** | ||||
| * Renders the `template` with the given `view` and `partials` using the | * Renders the `template` with the given `view` and `partials` using the | ||||
| * default writer. | |||||
| * default writer. If the optional `tags` argument is given here it must be an | |||||
| * array with two string values: the opening and closing tags used in the | |||||
| * template (e.g. [ "<%", "%>" ]). The default is to mustache.tags. | |||||
| */ | */ | ||||
| mustache.render = function render (template, view, partials) { | |||||
| mustache.render = function render (template, view, partials, tags) { | |||||
| if (typeof template !== 'string') { | if (typeof template !== 'string') { | ||||
| throw new TypeError('Invalid template! Template should be a "string" ' + | throw new TypeError('Invalid template! Template should be a "string" ' + | ||||
| 'but "' + typeStr(template) + '" was given as the first ' + | 'but "' + typeStr(template) + '" was given as the first ' + | ||||
| 'argument for mustache#render(template, view, partials)'); | 'argument for mustache#render(template, view, partials)'); | ||||
| } | } | ||||
| return defaultWriter.render(template, view, partials); | |||||
| return defaultWriter.render(template, view, partials, tags); | |||||
| }; | }; | ||||
| // This is here for backwards compatibility with 0.4.x., | // This is here for backwards compatibility with 0.4.x., | ||||
| @@ -17,6 +17,38 @@ describe('Mustache.render', function () { | |||||
| 'for mustache#render(template, view, partials)'); | 'for mustache#render(template, view, partials)'); | ||||
| }); | }); | ||||
| it('uses tags argument instead of Mustache.tags when given', function () { | |||||
| var template = '<<placeholder>>bar{{placeholder}}'; | |||||
| Mustache.tags = ['{{', '}}']; | |||||
| assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']), 'foobar{{placeholder}}'); | |||||
| }); | |||||
| it('uses tags argument instead of Mustache.tags when given, even when it previous rendered the template using Mustache.tags', function () { | |||||
| var template = '((placeholder))bar{{placeholder}}'; | |||||
| Mustache.tags = ['{{', '}}']; | |||||
| Mustache.render(template, { placeholder: 'foo' }); | |||||
| assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['((', '))']), 'foobar{{placeholder}}'); | |||||
| }); | |||||
| it('uses tags argument instead of Mustache.tags when given, even when it previous rendered the template using different tags', function () { | |||||
| var template = '[[placeholder]]bar<<placeholder>>'; | |||||
| Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']); | |||||
| assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['[[', ']]']), 'foobar<<placeholder>>'); | |||||
| }); | |||||
| it('does not mutate Mustache.tags when given tags argument', function() { | |||||
| var correctMustacheTags = ['{{', '}}']; | |||||
| Mustache.tags = correctMustacheTags; | |||||
| Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, ['((', '))']); | |||||
| assert.equal(Mustache.tags, correctMustacheTags); | |||||
| assert.deepEqual(Mustache.tags, ['{{', '}}']); | |||||
| }); | |||||
| tests.forEach(function (test) { | tests.forEach(function (test) { | ||||
| var view = eval(test.view); | var view = eval(test.view); | ||||