Skip to content

Loading…

Add the concept of default options to `Y.Template`. #368

Merged
merged 3 commits into from

3 participants

@ericf
YUI Library member

This adds a Y.Template(engine, defaults) signature so that common options can be specified as defaults and will be used when calling the instance methods, e.g., compile(), precompile(), render().

@rgrove rgrove commented on an outdated diff
src/template/js/template-base.js
@@ -74,6 +84,12 @@ Template.prototype = {
@since 3.8.0
**/
compile: function (text, options) {
+ var defaults = this.defaults;
+
+ if (defaults) {
+ options = options ? Y.merge(defaults, options) : defaults;
+ }
@rgrove
rgrove added a note

This could be simplified to a single line:

options = options ? Y.merge(this.defaults, options) : this.defaults;

Y.merge() ignores undefined arguments, so this ends up having the same behavior.

@ericf YUI Library member
ericf added a note

Good point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@davglass
YUI Library member

:thumbsup:

@ericf ericf merged commit 1d3c09f into yui:dev-master

1 check failed

Details default The Travis build failed
@ericf ericf deleted the ericf:template-defaults branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
7 src/template/HISTORY.md
@@ -1,6 +1,13 @@
Template Change History
=======================
+3.8.1
+-----
+
+* Added the concept of default options to `Y.Template` via the following
+ constructor signature: `Y.Template(engine, defaults)`.
+
+
3.8.0
-----
View
4 src/template/docs/index.mustache
@@ -648,7 +648,7 @@ The following example will define a Handlebars-like Micro template syntax:
Y.mix(Y.Template.Micro.options, {
code : /\{\{%([\s\S]+?)%\}\}/g,
escapedOutput: /\{\{(?!%)([\s\S]+?)\}\}/g,
- rawOutput : /\{\{\{([\s\S]+?)\}\}/g
+ rawOutput : /\{\{\{([\s\S]+?)\}\}\}/g
}, true);
```
@@ -710,7 +710,7 @@ Y.mix(Y.Template.Micro.options, {
</table>
<p>
-<strong>Note:</strong> The syntax identifiers can be specified on a per-template basis by passing `options` as the second argument to the `compile()`, `precompile()`, or `render()` methods.
+<strong>Note:</strong> The syntax identifiers can be specified on a per-template basis by passing `options` as the second argument to the `compile()`, `precompile()`, or `render()` methods. Alternatively you can specify `defaults` when using the [[#Generic Template API|generic Template API]], doing so will only affect how the templates are process for that engine instance.
</p>
<h2>Using Templates in Custom Components</h2>
View
18 src/template/js/template-base.js
@@ -43,11 +43,21 @@ Using with Handlebars:
@param {Mixed} [engine=Y.Template.Micro] Template engine to use, such as
`Y.Template.Micro` or `Y.Handlebars`. Defaults to `Y.Template.Micro` if not
specified.
+@param {Object} [defaults] Default options to use when instance methods are
+ invoked.
@constructor
@since 3.8.0
**/
-function Template(engine) {
+function Template(engine, defaults) {
+ /**
+ Default options.
+
+ @property {Object} defaults
+ @since 3.8.1
+ **/
+ this.defaults = defaults;
+
/**
Template engine class.
@@ -74,6 +84,7 @@ Template.prototype = {
@since 3.8.0
**/
compile: function (text, options) {
+ options = options ? Y.merge(this.defaults, options) : this.defaults;
return this.engine.compile(text, options);
},
@@ -89,6 +100,7 @@ Template.prototype = {
@since 3.8.0
**/
precompile: function (text, options) {
+ options = options ? Y.merge(this.defaults, options) : this.defaults;
return this.engine.precompile(text, options);
},
@@ -105,6 +117,8 @@ Template.prototype = {
@since 3.8.0
**/
render: function (text, data, options) {
+ options = options ? Y.merge(this.defaults, options) : this.defaults;
+
if (this.engine.render) {
return this.engine.render(text, data, options);
}
@@ -125,6 +139,8 @@ Template.prototype = {
@since 3.8.0
**/
revive: function (precompiled, options) {
+ options = options ? Y.merge(this.defaults, options) : this.defaults;
+
return this.engine.revive ? this.engine.revive(precompiled, options) :
precompiled;
}
View
85 src/template/tests/unit/assets/template-test.js
@@ -234,6 +234,91 @@ templateSuite.add(new Y.Test.Case({
}
}));
+templateSuite.add(new Y.Test.Case({
+ name: 'Options',
+
+ setUp: function () {
+ // Creates a Template.Micro engine with specificed defaults.
+ this.microEngine = new Y.Template(Y.Template.Micro, {
+ code : /\{\{%([\s\S]+?)%\}\}/g,
+ escapedOutput: /\{\{(?!%)([\s\S]+?)\}\}/g,
+ rawOutput : /\{\{\{([\s\S]+?)\}\}\}/g
+ });
+
+ // Creates a Handlebars engine with specificed defaults.
+ this.hbEngine = new Y.Template(Y.Handlebars, {
+ partials: {
+ foo: '!{{a}}!'
+ }
+ });
+ },
+
+ tearDown: function () {
+ delete this.microEngine;
+ delete this.hbEngine;
+ },
+
+ 'compile() should compile a template using the specified `defaults`': function () {
+ Assert.areSame('foo', this.microEngine.compile('{{this.a}}')({a: 'foo'}), 'should compile Micro templates with custom syntax');
+ Assert.areSame('!foo!', this.hbEngine.compile('{{> foo}}')({a: 'foo'}, this.hbEngine.defaults), 'should compile Handlebars template with default partials');
+ },
+
+ 'compile() should merge `defaults` with specificed `options`': function () {
+ Assert.areSame(
+ 'foo',
+ this.microEngine.compile('<%= data.a %>', {
+ escapedOutput: /<%=([\s\S]+?)%>/g
+ })({a: 'foo'})
+ );
+ },
+
+ 'precompile() should precompile a template using the specified `defaults`': function () {
+ Assert.areSame(
+ "function (Y, $e, data) {\nvar $b='',$t=''+\n$e((data.test)||$b)+\n'';\nreturn $t;\n}",
+ this.microEngine.precompile('{{data.test}}')
+ );
+ },
+
+ 'precompile() should merge `defaults` with the specified `options`': function () {
+ Assert.areSame(
+ "function (Y, $e, data) {\nvar $b='',$t=''+\n$e((data.test)||$b)+\n'';\nreturn $t;\n}",
+
+ this.microEngine.precompile('<%=data.test%>', {
+ escapedOutput: /<%=([\s\S]+?)%>/g
+ })
+ );
+ },
+
+ 'render() should compile and render a template using the the specified `defaults`': function () {
+ Assert.areSame(
+ 'foo',
+ this.microEngine.render('{{data.a}}', {a: 'foo'})
+ );
+ },
+
+ 'render() should merge `defaults` with the specified `options`': function () {
+ Assert.areSame(
+ 'foo bar',
+ this.microEngine.render('<%= data.a %> {{{ data.b }}}', {
+ a: 'foo',
+ b: 'bar'
+ }, {
+ escapedOutput: /<%=([\s\S]+?)%>/g
+ })
+ );
+ },
+
+ 'revive() should revive a precompiled template using the specified `defaults`': function () {
+ eval('var precompiled = ' + this.microEngine.precompile('{{data.a}}') + ';');
+ Assert.areSame('foo', this.microEngine.revive(precompiled)({a: 'foo'}));
+ },
+
+ 'revive() should merge `defaults` with the specified `options`': function () {
+ eval('var precompiled = ' + this.microEngine.precompile('{{data.a}}') + ';');
+ Assert.areSame('foo', this.microEngine.revive(precompiled, {})({a: 'foo'}));
+ }
+}));
+
}, '@VERSION@', {
requires: ['handlebars', 'template', 'test']
});
Something went wrong with that request. Please try again.