|
1 YUI.add('handlebars-base', function (Y, NAME) { |
|
2 |
|
3 /*! |
|
4 Handlebars.js - Copyright (C) 2011 Yehuda Katz |
|
5 https://raw.github.com/wycats/handlebars.js/master/LICENSE |
|
6 */ |
|
7 // This file contains YUI-specific wrapper code and overrides for the |
|
8 // handlebars-base module. |
|
9 |
|
10 /** |
|
11 Handlebars is a simple template language inspired by Mustache. |
|
12 |
|
13 This is a YUI port of the original Handlebars project, which can be found at |
|
14 <https://github.com/wycats/handlebars.js>. |
|
15 |
|
16 @module handlebars |
|
17 @main handlebars |
|
18 @since 3.5.0 |
|
19 */ |
|
20 |
|
21 /** |
|
22 Provides basic Handlebars template rendering functionality. Use this module when |
|
23 you only need to render pre-compiled templates. |
|
24 |
|
25 @module handlebars |
|
26 @submodule handlebars-base |
|
27 */ |
|
28 |
|
29 /** |
|
30 Handlebars is a simple template language inspired by Mustache. |
|
31 |
|
32 This is a YUI port of the original Handlebars project, which can be found at |
|
33 <https://github.com/wycats/handlebars.js>. |
|
34 |
|
35 @class Handlebars |
|
36 @since 3.5.0 |
|
37 */ |
|
38 var Handlebars = Y.namespace('Handlebars'); |
|
39 /* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */ |
|
40 |
|
41 Handlebars.VERSION = "1.0.0"; |
|
42 Handlebars.COMPILER_REVISION = 4; |
|
43 |
|
44 Handlebars.REVISION_CHANGES = { |
|
45 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it |
|
46 2: '== 1.0.0-rc.3', |
|
47 3: '== 1.0.0-rc.4', |
|
48 4: '>= 1.0.0' |
|
49 }; |
|
50 |
|
51 Handlebars.helpers = {}; |
|
52 Handlebars.partials = {}; |
|
53 |
|
54 var toString = Object.prototype.toString, |
|
55 functionType = '[object Function]', |
|
56 objectType = '[object Object]'; |
|
57 |
|
58 Handlebars.registerHelper = function(name, fn, inverse) { |
|
59 if (toString.call(name) === objectType) { |
|
60 if (inverse || fn) { throw new Handlebars.Exception('Arg not supported with multiple helpers'); } |
|
61 Handlebars.Utils.extend(this.helpers, name); |
|
62 } else { |
|
63 if (inverse) { fn.not = inverse; } |
|
64 this.helpers[name] = fn; |
|
65 } |
|
66 }; |
|
67 |
|
68 Handlebars.registerPartial = function(name, str) { |
|
69 if (toString.call(name) === objectType) { |
|
70 Handlebars.Utils.extend(this.partials, name); |
|
71 } else { |
|
72 this.partials[name] = str; |
|
73 } |
|
74 }; |
|
75 |
|
76 Handlebars.registerHelper('helperMissing', function(arg) { |
|
77 if(arguments.length === 2) { |
|
78 return undefined; |
|
79 } else { |
|
80 throw new Error("Missing helper: '" + arg + "'"); |
|
81 } |
|
82 }); |
|
83 |
|
84 Handlebars.registerHelper('blockHelperMissing', function(context, options) { |
|
85 var inverse = options.inverse || function() {}, fn = options.fn; |
|
86 |
|
87 var type = toString.call(context); |
|
88 |
|
89 if(type === functionType) { context = context.call(this); } |
|
90 |
|
91 if(context === true) { |
|
92 return fn(this); |
|
93 } else if(context === false || context == null) { |
|
94 return inverse(this); |
|
95 } else if(type === "[object Array]") { |
|
96 if(context.length > 0) { |
|
97 return Handlebars.helpers.each(context, options); |
|
98 } else { |
|
99 return inverse(this); |
|
100 } |
|
101 } else { |
|
102 return fn(context); |
|
103 } |
|
104 }); |
|
105 |
|
106 Handlebars.K = function() {}; |
|
107 |
|
108 Handlebars.createFrame = Object.create || function(object) { |
|
109 Handlebars.K.prototype = object; |
|
110 var obj = new Handlebars.K(); |
|
111 Handlebars.K.prototype = null; |
|
112 return obj; |
|
113 }; |
|
114 |
|
115 Handlebars.logger = { |
|
116 DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3, |
|
117 |
|
118 methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'}, |
|
119 |
|
120 // can be overridden in the host environment |
|
121 log: function(level, obj) { |
|
122 if (Handlebars.logger.level <= level) { |
|
123 var method = Handlebars.logger.methodMap[level]; |
|
124 if (typeof console !== 'undefined' && console[method]) { |
|
125 console[method].call(console, obj); |
|
126 } |
|
127 } |
|
128 } |
|
129 }; |
|
130 |
|
131 Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); }; |
|
132 |
|
133 Handlebars.registerHelper('each', function(context, options) { |
|
134 var fn = options.fn, inverse = options.inverse; |
|
135 var i = 0, ret = "", data; |
|
136 |
|
137 var type = toString.call(context); |
|
138 if(type === functionType) { context = context.call(this); } |
|
139 |
|
140 if (options.data) { |
|
141 data = Handlebars.createFrame(options.data); |
|
142 } |
|
143 |
|
144 if(context && typeof context === 'object') { |
|
145 if(context instanceof Array){ |
|
146 for(var j = context.length; i<j; i++) { |
|
147 if (data) { data.index = i; } |
|
148 ret = ret + fn(context[i], { data: data }); |
|
149 } |
|
150 } else { |
|
151 for(var key in context) { |
|
152 if(context.hasOwnProperty(key)) { |
|
153 if(data) { data.key = key; } |
|
154 ret = ret + fn(context[key], {data: data}); |
|
155 i++; |
|
156 } |
|
157 } |
|
158 } |
|
159 } |
|
160 |
|
161 if(i === 0){ |
|
162 ret = inverse(this); |
|
163 } |
|
164 |
|
165 return ret; |
|
166 }); |
|
167 |
|
168 Handlebars.registerHelper('if', function(conditional, options) { |
|
169 var type = toString.call(conditional); |
|
170 if(type === functionType) { conditional = conditional.call(this); } |
|
171 |
|
172 if(!conditional || Handlebars.Utils.isEmpty(conditional)) { |
|
173 return options.inverse(this); |
|
174 } else { |
|
175 return options.fn(this); |
|
176 } |
|
177 }); |
|
178 |
|
179 Handlebars.registerHelper('unless', function(conditional, options) { |
|
180 return Handlebars.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn}); |
|
181 }); |
|
182 |
|
183 Handlebars.registerHelper('with', function(context, options) { |
|
184 var type = toString.call(context); |
|
185 if(type === functionType) { context = context.call(this); } |
|
186 |
|
187 if (!Handlebars.Utils.isEmpty(context)) return options.fn(context); |
|
188 }); |
|
189 |
|
190 Handlebars.registerHelper('log', function(context, options) { |
|
191 var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1; |
|
192 Handlebars.log(level, context); |
|
193 }); |
|
194 /* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */ |
|
195 |
|
196 var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack']; |
|
197 |
|
198 Handlebars.Exception = function(message) { |
|
199 var tmp = Error.prototype.constructor.apply(this, arguments); |
|
200 |
|
201 // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work. |
|
202 for (var idx = 0; idx < errorProps.length; idx++) { |
|
203 this[errorProps[idx]] = tmp[errorProps[idx]]; |
|
204 } |
|
205 }; |
|
206 Handlebars.Exception.prototype = new Error(); |
|
207 |
|
208 // Build out our basic SafeString type |
|
209 Handlebars.SafeString = function(string) { |
|
210 this.string = string; |
|
211 }; |
|
212 Handlebars.SafeString.prototype.toString = function() { |
|
213 return this.string.toString(); |
|
214 }; |
|
215 |
|
216 var escape = { |
|
217 "&": "&", |
|
218 "<": "<", |
|
219 ">": ">", |
|
220 '"': """, |
|
221 "'": "'", |
|
222 "`": "`" |
|
223 }; |
|
224 |
|
225 var badChars = /[&<>"'`]/g; |
|
226 var possible = /[&<>"'`]/; |
|
227 |
|
228 var escapeChar = function(chr) { |
|
229 return escape[chr] || "&"; |
|
230 }; |
|
231 |
|
232 Handlebars.Utils = { |
|
233 extend: function(obj, value) { |
|
234 for(var key in value) { |
|
235 if(value.hasOwnProperty(key)) { |
|
236 obj[key] = value[key]; |
|
237 } |
|
238 } |
|
239 }, |
|
240 |
|
241 escapeExpression: function(string) { |
|
242 // don't escape SafeStrings, since they're already safe |
|
243 if (string instanceof Handlebars.SafeString) { |
|
244 return string.toString(); |
|
245 } else if (string == null || string === false) { |
|
246 return ""; |
|
247 } |
|
248 |
|
249 // Force a string conversion as this will be done by the append regardless and |
|
250 // the regex test will do this transparently behind the scenes, causing issues if |
|
251 // an object's to string has escaped characters in it. |
|
252 string = string.toString(); |
|
253 |
|
254 if(!possible.test(string)) { return string; } |
|
255 return string.replace(badChars, escapeChar); |
|
256 }, |
|
257 |
|
258 isEmpty: function(value) { |
|
259 if (!value && value !== 0) { |
|
260 return true; |
|
261 } else if(toString.call(value) === "[object Array]" && value.length === 0) { |
|
262 return true; |
|
263 } else { |
|
264 return false; |
|
265 } |
|
266 } |
|
267 }; |
|
268 /* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */ |
|
269 |
|
270 Handlebars.VM = { |
|
271 template: function(templateSpec) { |
|
272 // Just add water |
|
273 var container = { |
|
274 escapeExpression: Handlebars.Utils.escapeExpression, |
|
275 invokePartial: Handlebars.VM.invokePartial, |
|
276 programs: [], |
|
277 program: function(i, fn, data) { |
|
278 var programWrapper = this.programs[i]; |
|
279 if(data) { |
|
280 programWrapper = Handlebars.VM.program(i, fn, data); |
|
281 } else if (!programWrapper) { |
|
282 programWrapper = this.programs[i] = Handlebars.VM.program(i, fn); |
|
283 } |
|
284 return programWrapper; |
|
285 }, |
|
286 merge: function(param, common) { |
|
287 var ret = param || common; |
|
288 |
|
289 if (param && common) { |
|
290 ret = {}; |
|
291 Handlebars.Utils.extend(ret, common); |
|
292 Handlebars.Utils.extend(ret, param); |
|
293 } |
|
294 return ret; |
|
295 }, |
|
296 programWithDepth: Handlebars.VM.programWithDepth, |
|
297 noop: Handlebars.VM.noop, |
|
298 compilerInfo: null |
|
299 }; |
|
300 |
|
301 return function(context, options) { |
|
302 options = options || {}; |
|
303 var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); |
|
304 |
|
305 var compilerInfo = container.compilerInfo || [], |
|
306 compilerRevision = compilerInfo[0] || 1, |
|
307 currentRevision = Handlebars.COMPILER_REVISION; |
|
308 |
|
309 if (compilerRevision !== currentRevision) { |
|
310 if (compilerRevision < currentRevision) { |
|
311 var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision], |
|
312 compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision]; |
|
313 throw "Template was precompiled with an older version of Handlebars than the current runtime. "+ |
|
314 "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+")."; |
|
315 } else { |
|
316 // Use the embedded version info since the runtime doesn't know about this revision yet |
|
317 throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+ |
|
318 "Please update your runtime to a newer version ("+compilerInfo[1]+")."; |
|
319 } |
|
320 } |
|
321 |
|
322 return result; |
|
323 }; |
|
324 }, |
|
325 |
|
326 programWithDepth: function(i, fn, data /*, $depth */) { |
|
327 var args = Array.prototype.slice.call(arguments, 3); |
|
328 |
|
329 var program = function(context, options) { |
|
330 options = options || {}; |
|
331 |
|
332 return fn.apply(this, [context, options.data || data].concat(args)); |
|
333 }; |
|
334 program.program = i; |
|
335 program.depth = args.length; |
|
336 return program; |
|
337 }, |
|
338 program: function(i, fn, data) { |
|
339 var program = function(context, options) { |
|
340 options = options || {}; |
|
341 |
|
342 return fn(context, options.data || data); |
|
343 }; |
|
344 program.program = i; |
|
345 program.depth = 0; |
|
346 return program; |
|
347 }, |
|
348 noop: function() { return ""; }, |
|
349 invokePartial: function(partial, name, context, helpers, partials, data) { |
|
350 var options = { helpers: helpers, partials: partials, data: data }; |
|
351 |
|
352 if(partial === undefined) { |
|
353 throw new Handlebars.Exception("The partial " + name + " could not be found"); |
|
354 } else if(partial instanceof Function) { |
|
355 return partial(context, options); |
|
356 } else if (!Handlebars.compile) { |
|
357 throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); |
|
358 } else { |
|
359 partials[name] = Handlebars.compile(partial, {data: data !== undefined}); |
|
360 return partials[name](context, options); |
|
361 } |
|
362 } |
|
363 }; |
|
364 |
|
365 Handlebars.template = Handlebars.VM.template; |
|
366 // This file contains YUI-specific wrapper code and overrides for the |
|
367 // handlebars-base module. |
|
368 |
|
369 Handlebars.VERSION += '-yui'; |
|
370 |
|
371 /** |
|
372 Registers a helper function that will be made available to all templates. |
|
373 |
|
374 Helper functions receive the current template context as the `this` object, and |
|
375 can also receive arguments passed by the template. |
|
376 |
|
377 @example |
|
378 |
|
379 Y.Handlebars.registerHelper('linkify', function () { |
|
380 return '<a href="' + Y.Escape.html(this.url) + '">' + |
|
381 Y.Escape.html(this.text) + '</a>'; |
|
382 }); |
|
383 |
|
384 var source = '<ul>{{#links}}<li>{{{linkify}}}</li>{{/links}}</ul>'; |
|
385 |
|
386 Y.Handlebars.render(source, { |
|
387 links: [ |
|
388 {url: '/foo', text: 'Foo'}, |
|
389 {url: '/bar', text: 'Bar'}, |
|
390 {url: '/baz', text: 'Baz'} |
|
391 ] |
|
392 }); |
|
393 |
|
394 @method registerHelper |
|
395 @param {String} name Name of this helper. |
|
396 @param {Function} fn Helper function. |
|
397 @param {Boolean} [inverse=false] If `true`, this helper will be considered an |
|
398 "inverse" helper, like "unless". This means it will only be called if the |
|
399 expression given in the template evaluates to a false or empty value. |
|
400 */ |
|
401 |
|
402 /** |
|
403 Registers a partial that will be made available to all templates. |
|
404 |
|
405 A partial is another template that can be used to render part of a larger |
|
406 template. For example, a website with a common header and footer across all its |
|
407 pages might use a template for each page, which would call shared partials to |
|
408 render the headers and footers. |
|
409 |
|
410 Partials may be specified as uncompiled template strings or as compiled template |
|
411 functions. |
|
412 |
|
413 @example |
|
414 |
|
415 Y.Handlebars.registerPartial('header', '<h1>{{title}}</h1>'); |
|
416 Y.Handlebars.registerPartial('footer', 'Copyright (c) 2011 by Me.'); |
|
417 |
|
418 var source = '{{> header}} <p>Mustaches are awesome!</p> {{> footer}}'; |
|
419 |
|
420 Y.Handlebars.render(source, {title: 'My Page About Mustaches'}); |
|
421 |
|
422 @method registerPartial |
|
423 @param {String} name Name of this partial. |
|
424 @param {Function|String} partial Template string or compiled template function. |
|
425 */ |
|
426 |
|
427 /** |
|
428 Converts a precompiled template into a renderable template function. |
|
429 |
|
430 @example |
|
431 |
|
432 <script src="precompiled-template.js"></script> |
|
433 <script> |
|
434 YUI().use('handlebars-base', function (Y) { |
|
435 // Convert the precompiled template function into a renderable template |
|
436 // function. |
|
437 var template = Y.Handlebars.template(precompiledTemplate); |
|
438 |
|
439 // Render it. |
|
440 template({pie: 'Pumpkin'}); |
|
441 }); |
|
442 </script> |
|
443 |
|
444 @method template |
|
445 @param {Function} template Precompiled Handlebars template function. |
|
446 @return {Function} Compiled template function. |
|
447 */ |
|
448 |
|
449 // Alias for Y.Handlebars.template(), used by Y.Template. |
|
450 Handlebars.revive = Handlebars.template; |
|
451 |
|
452 // Make Y.Template.Handlebars an alias for Y.Handlebars. |
|
453 Y.namespace('Template').Handlebars = Handlebars; |
|
454 |
|
455 |
|
456 }, '@VERSION@', {"requires": []}); |