|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('template-base', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 Virtual rollup of the `template-base` and `template-micro` modules. |
|
12 |
|
13 @module template |
|
14 @main template |
|
15 @since 3.8.0 |
|
16 **/ |
|
17 |
|
18 /** |
|
19 Provides a generic API for using template engines such as Handlebars and |
|
20 `Y.Template.Micro`. |
|
21 |
|
22 @module template |
|
23 @submodule template-base |
|
24 @since 3.8.0 |
|
25 **/ |
|
26 |
|
27 /** |
|
28 Provides a generic API for using template engines such as Handlebars and |
|
29 `Y.Template.Micro`. |
|
30 |
|
31 ### Examples |
|
32 |
|
33 Using with `Y.Template.Micro` (the default template engine): |
|
34 |
|
35 YUI().use('template', function (Y) { |
|
36 var micro = new Y.Template(), |
|
37 html = micro.render('<%= data.message %>', {message: 'hello!'}); |
|
38 |
|
39 // ... |
|
40 }); |
|
41 |
|
42 Using with Handlebars: |
|
43 |
|
44 YUI().use('template-base', 'handlebars', function (Y) { |
|
45 var handlebars = new Y.Template(Y.Handlebars), |
|
46 html = handlebars.render('{{message}}', {message: 'hello!'}); |
|
47 |
|
48 // ... |
|
49 }); |
|
50 |
|
51 @class Template |
|
52 @param {Mixed} [engine=Y.Template.Micro] Template engine to use, such as |
|
53 `Y.Template.Micro` or `Y.Handlebars`. Defaults to `Y.Template.Micro` if not |
|
54 specified. |
|
55 @param {Object} [defaults] Default options to use when instance methods are |
|
56 invoked. |
|
57 @constructor |
|
58 @since 3.8.0 |
|
59 **/ |
|
60 |
|
61 function Template(engine, defaults) { |
|
62 /** |
|
63 Default options. |
|
64 |
|
65 @property {Object} defaults |
|
66 @since 3.8.1 |
|
67 **/ |
|
68 this.defaults = defaults; |
|
69 |
|
70 /** |
|
71 Template engine class. |
|
72 |
|
73 @property {Mixed} engine |
|
74 @since 3.8.0 |
|
75 **/ |
|
76 this.engine = engine || Y.Template.Micro; |
|
77 |
|
78 if (!this.engine) { |
|
79 Y.error('No template engine loaded.'); |
|
80 } |
|
81 } |
|
82 |
|
83 Template.prototype = { |
|
84 /** |
|
85 Compiles a template with the current template engine and returns a compiled |
|
86 template function. |
|
87 |
|
88 @method compile |
|
89 @param {String} text Template text to compile. |
|
90 @param {Object} [options] Options to pass along to the template engine. See |
|
91 template engine docs for options supported by each engine. |
|
92 @return {Function} Compiled template function. |
|
93 @since 3.8.0 |
|
94 **/ |
|
95 compile: function (text, options) { |
|
96 options = options ? Y.merge(this.defaults, options) : this.defaults; |
|
97 return this.engine.compile(text, options); |
|
98 }, |
|
99 |
|
100 /** |
|
101 Precompiles a template with the current template engine and returns a string |
|
102 containing JavaScript source code for the precompiled template. |
|
103 |
|
104 @method precompile |
|
105 @param {String} text Template text to compile. |
|
106 @param {Object} [options] Options to pass along to the template engine. See |
|
107 template engine docs for options supported by each engine. |
|
108 @return {String} Source code for the precompiled template. |
|
109 @since 3.8.0 |
|
110 **/ |
|
111 precompile: function (text, options) { |
|
112 options = options ? Y.merge(this.defaults, options) : this.defaults; |
|
113 return this.engine.precompile(text, options); |
|
114 }, |
|
115 |
|
116 /** |
|
117 Compiles and renders a template with the current template engine in a single |
|
118 step, and returns the rendered result. |
|
119 |
|
120 @method render |
|
121 @param {String} text Template text to render. |
|
122 @param {Object} data Data object to provide when rendering the template. |
|
123 @param {Object} [options] Options to pass along to the template engine. See |
|
124 template engine docs for options supported by each engine. |
|
125 @return {String} Rendered result. |
|
126 @since 3.8.0 |
|
127 **/ |
|
128 render: function (text, data, options) { |
|
129 options = options ? Y.merge(this.defaults, options) : this.defaults; |
|
130 |
|
131 if (this.engine.render) { |
|
132 return this.engine.render(text, data, options); |
|
133 } |
|
134 |
|
135 return this.engine.compile(text, options)(data, options); |
|
136 }, |
|
137 |
|
138 /** |
|
139 Revives a precompiled template function into an executable template function |
|
140 using the current template engine. The precompiled code must already have |
|
141 been evaluated; this method won't evaluate it for you. |
|
142 |
|
143 @method revive |
|
144 @param {Function} precompiled Precompiled template function. |
|
145 @param {Object} [options] Options to pass along to the template engine. See |
|
146 template engine docs for options supported by each engine. |
|
147 @return {Function} Compiled template function. |
|
148 @since 3.8.0 |
|
149 **/ |
|
150 revive: function (precompiled, options) { |
|
151 options = options ? Y.merge(this.defaults, options) : this.defaults; |
|
152 |
|
153 return this.engine.revive ? this.engine.revive(precompiled, options) : |
|
154 precompiled; |
|
155 } |
|
156 }; |
|
157 |
|
158 // Copy existing namespaced properties from Y.Template to the Template function |
|
159 // if Y.Template already exists, then make the function the new Y.Template. |
|
160 // This ensures that other modules can safely add stuff to the Y.Template |
|
161 // namespace even if they're loaded before this one. |
|
162 Y.Template = Y.Template ? Y.mix(Template, Y.Template) : Template; |
|
163 |
|
164 |
|
165 }, '3.10.3', {"requires": ["yui-base"]}); |