|
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('handlebars-base', function (Y, NAME) { |
|
9 |
|
10 /*! |
|
11 Handlebars.js - Copyright (C) 2011 Yehuda Katz |
|
12 https://raw.github.com/wycats/handlebars.js/master/LICENSE |
|
13 */ |
|
14 // This file contains YUI-specific wrapper code and overrides for the |
|
15 // handlebars-base module. |
|
16 |
|
17 /** |
|
18 Handlebars is a simple template language inspired by Mustache. |
|
19 |
|
20 This is a YUI port of the original Handlebars project, which can be found at |
|
21 <https://github.com/wycats/handlebars.js>. |
|
22 |
|
23 @module handlebars |
|
24 @main handlebars |
|
25 @since 3.5.0 |
|
26 */ |
|
27 |
|
28 /** |
|
29 Provides basic Handlebars template rendering functionality. Use this module when |
|
30 you only need to render pre-compiled templates. |
|
31 |
|
32 @module handlebars |
|
33 @submodule handlebars-base |
|
34 */ |
|
35 |
|
36 /** |
|
37 Handlebars is a simple template language inspired by Mustache. |
|
38 |
|
39 This is a YUI port of the original Handlebars project, which can be found at |
|
40 <https://github.com/wycats/handlebars.js>. |
|
41 |
|
42 @class Handlebars |
|
43 @since 3.5.0 |
|
44 */ |
|
45 var Handlebars = Y.namespace('Handlebars'); |
|
46 /* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */ |
|
47 |
|
48 Handlebars.VERSION = "1.0.0-rc.4"; |
|
49 Handlebars.COMPILER_REVISION = 3; |
|
50 |
|
51 Handlebars.REVISION_CHANGES = { |
|
52 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it |
|
53 2: '== 1.0.0-rc.3', |
|
54 3: '>= 1.0.0-rc.4' |
|
55 }; |
|
56 |
|
57 Handlebars.helpers = {}; |
|
58 Handlebars.partials = {}; |
|
59 |
|
60 var toString = Object.prototype.toString, |
|
61 functionType = '[object Function]', |
|
62 objectType = '[object Object]'; |
|
63 |
|
64 Handlebars.registerHelper = function(name, fn, inverse) { |
|
65 if (toString.call(name) === objectType) { |
|
66 if (inverse || fn) { throw new Handlebars.Exception('Arg not supported with multiple helpers'); } |
|
67 Handlebars.Utils.extend(this.helpers, name); |
|
68 } else { |
|
69 if (inverse) { fn.not = inverse; } |
|
70 this.helpers[name] = fn; |
|
71 } |
|
72 }; |
|
73 |
|
74 Handlebars.registerPartial = function(name, str) { |
|
75 if (toString.call(name) === objectType) { |
|
76 Handlebars.Utils.extend(this.partials, name); |
|
77 } else { |
|
78 this.partials[name] = str; |
|
79 } |
|
80 }; |
|
81 |
|
82 Handlebars.registerHelper('helperMissing', function(arg) { |
|
83 if(arguments.length === 2) { |
|
84 return undefined; |
|
85 } else { |
|
86 throw new Error("Could not find property '" + arg + "'"); |
|
87 } |
|
88 }); |
|
89 |
|
90 Handlebars.registerHelper('blockHelperMissing', function(context, options) { |
|
91 var inverse = options.inverse || function() {}, fn = options.fn; |
|
92 |
|
93 var type = toString.call(context); |
|
94 |
|
95 if(type === functionType) { context = context.call(this); } |
|
96 |
|
97 if(context === true) { |
|
98 return fn(this); |
|
99 } else if(context === false || context == null) { |
|
100 return inverse(this); |
|
101 } else if(type === "[object Array]") { |
|
102 if(context.length > 0) { |
|
103 return Handlebars.helpers.each(context, options); |
|
104 } else { |
|
105 return inverse(this); |
|
106 } |
|
107 } else { |
|
108 return fn(context); |
|
109 } |
|
110 }); |
|
111 |
|
112 Handlebars.K = function() {}; |
|
113 |
|
114 Handlebars.createFrame = Object.create || function(object) { |
|
115 Handlebars.K.prototype = object; |
|
116 var obj = new Handlebars.K(); |
|
117 Handlebars.K.prototype = null; |
|
118 return obj; |
|
119 }; |
|
120 |
|
121 Handlebars.logger = { |
|
122 DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3, |
|
123 |
|
124 methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'}, |
|
125 |
|
126 // can be overridden in the host environment |
|
127 log: function(level, obj) { |
|
128 if (Handlebars.logger.level <= level) { |
|
129 var method = Handlebars.logger.methodMap[level]; |
|
130 if (typeof console !== 'undefined' && console[method]) { |
|
131 console[method].call(console, obj); |
|
132 } |
|
133 } |
|
134 } |
|
135 }; |
|
136 |
|
137 Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); }; |
|
138 |
|
139 Handlebars.registerHelper('each', function(context, options) { |
|
140 var fn = options.fn, inverse = options.inverse; |
|
141 var i = 0, ret = "", data; |
|
142 |
|
143 if (options.data) { |
|
144 data = Handlebars.createFrame(options.data); |
|
145 } |
|
146 |
|
147 if(context && typeof context === 'object') { |
|
148 if(context instanceof Array){ |
|
149 for(var j = context.length; i<j; i++) { |
|
150 if (data) { data.index = i; } |
|
151 ret = ret + fn(context[i], { data: data }); |
|
152 } |
|
153 } else { |
|
154 for(var key in context) { |
|
155 if(context.hasOwnProperty(key)) { |
|
156 if(data) { data.key = key; } |
|
157 ret = ret + fn(context[key], {data: data}); |
|
158 i++; |
|
159 } |
|
160 } |
|
161 } |
|
162 } |
|
163 |
|
164 if(i === 0){ |
|
165 ret = inverse(this); |
|
166 } |
|
167 |
|
168 return ret; |
|
169 }); |
|
170 |
|
171 Handlebars.registerHelper('if', function(context, options) { |
|
172 var type = toString.call(context); |
|
173 if(type === functionType) { context = context.call(this); } |
|
174 |
|
175 if(!context || Handlebars.Utils.isEmpty(context)) { |
|
176 return options.inverse(this); |
|
177 } else { |
|
178 return options.fn(this); |
|
179 } |
|
180 }); |
|
181 |
|
182 Handlebars.registerHelper('unless', function(context, options) { |
|
183 return Handlebars.helpers['if'].call(this, context, {fn: options.inverse, inverse: options.fn}); |
|
184 }); |
|
185 |
|
186 Handlebars.registerHelper('with', function(context, options) { |
|
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 programWithDepth: Handlebars.VM.programWithDepth, |
|
287 noop: Handlebars.VM.noop, |
|
288 compilerInfo: null |
|
289 }; |
|
290 |
|
291 return function(context, options) { |
|
292 options = options || {}; |
|
293 var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); |
|
294 |
|
295 var compilerInfo = container.compilerInfo || [], |
|
296 compilerRevision = compilerInfo[0] || 1, |
|
297 currentRevision = Handlebars.COMPILER_REVISION; |
|
298 |
|
299 if (compilerRevision !== currentRevision) { |
|
300 if (compilerRevision < currentRevision) { |
|
301 var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision], |
|
302 compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision]; |
|
303 throw "Template was precompiled with an older version of Handlebars than the current runtime. "+ |
|
304 "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+")."; |
|
305 } else { |
|
306 // Use the embedded version info since the runtime doesn't know about this revision yet |
|
307 throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+ |
|
308 "Please update your runtime to a newer version ("+compilerInfo[1]+")."; |
|
309 } |
|
310 } |
|
311 |
|
312 return result; |
|
313 }; |
|
314 }, |
|
315 |
|
316 programWithDepth: function(i, fn, data /*, $depth */) { |
|
317 var args = Array.prototype.slice.call(arguments, 3); |
|
318 |
|
319 var program = function(context, options) { |
|
320 options = options || {}; |
|
321 |
|
322 return fn.apply(this, [context, options.data || data].concat(args)); |
|
323 }; |
|
324 program.program = i; |
|
325 program.depth = args.length; |
|
326 return program; |
|
327 }, |
|
328 program: function(i, fn, data) { |
|
329 var program = function(context, options) { |
|
330 options = options || {}; |
|
331 |
|
332 return fn(context, options.data || data); |
|
333 }; |
|
334 program.program = i; |
|
335 program.depth = 0; |
|
336 return program; |
|
337 }, |
|
338 noop: function() { return ""; }, |
|
339 invokePartial: function(partial, name, context, helpers, partials, data) { |
|
340 var options = { helpers: helpers, partials: partials, data: data }; |
|
341 |
|
342 if(partial === undefined) { |
|
343 throw new Handlebars.Exception("The partial " + name + " could not be found"); |
|
344 } else if(partial instanceof Function) { |
|
345 return partial(context, options); |
|
346 } else if (!Handlebars.compile) { |
|
347 throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); |
|
348 } else { |
|
349 partials[name] = Handlebars.compile(partial, {data: data !== undefined}); |
|
350 return partials[name](context, options); |
|
351 } |
|
352 } |
|
353 }; |
|
354 |
|
355 Handlebars.template = Handlebars.VM.template; |
|
356 // This file contains YUI-specific wrapper code and overrides for the |
|
357 // handlebars-base module. |
|
358 |
|
359 Handlebars.VERSION += '-yui'; |
|
360 |
|
361 /** |
|
362 Registers a helper function that will be made available to all templates. |
|
363 |
|
364 Helper functions receive the current template context as the `this` object, and |
|
365 can also receive arguments passed by the template. |
|
366 |
|
367 @example |
|
368 |
|
369 Y.Handlebars.registerHelper('linkify', function () { |
|
370 return '<a href="' + Y.Escape.html(this.url) + '">' + |
|
371 Y.Escape.html(this.text) + '</a>'; |
|
372 }); |
|
373 |
|
374 var source = '<ul>{{#links}}<li>{{{linkify}}}</li>{{/links}}</ul>'; |
|
375 |
|
376 Y.Handlebars.render(source, { |
|
377 links: [ |
|
378 {url: '/foo', text: 'Foo'}, |
|
379 {url: '/bar', text: 'Bar'}, |
|
380 {url: '/baz', text: 'Baz'} |
|
381 ] |
|
382 }); |
|
383 |
|
384 @method registerHelper |
|
385 @param {String} name Name of this helper. |
|
386 @param {Function} fn Helper function. |
|
387 @param {Boolean} [inverse=false] If `true`, this helper will be considered an |
|
388 "inverse" helper, like "unless". This means it will only be called if the |
|
389 expression given in the template evaluates to a false or empty value. |
|
390 */ |
|
391 |
|
392 /** |
|
393 Registers a partial that will be made available to all templates. |
|
394 |
|
395 A partial is another template that can be used to render part of a larger |
|
396 template. For example, a website with a common header and footer across all its |
|
397 pages might use a template for each page, which would call shared partials to |
|
398 render the headers and footers. |
|
399 |
|
400 Partials may be specified as uncompiled template strings or as compiled template |
|
401 functions. |
|
402 |
|
403 @example |
|
404 |
|
405 Y.Handlebars.registerPartial('header', '<h1>{{title}}</h1>'); |
|
406 Y.Handlebars.registerPartial('footer', 'Copyright (c) 2011 by Me.'); |
|
407 |
|
408 var source = '{{> header}} <p>Mustaches are awesome!</p> {{> footer}}'; |
|
409 |
|
410 Y.Handlebars.render(source, {title: 'My Page About Mustaches'}); |
|
411 |
|
412 @method registerPartial |
|
413 @param {String} name Name of this partial. |
|
414 @param {Function|String} partial Template string or compiled template function. |
|
415 */ |
|
416 |
|
417 /** |
|
418 Converts a precompiled template into a renderable template function. |
|
419 |
|
420 @example |
|
421 |
|
422 <script src="precompiled-template.js"></script> |
|
423 <script> |
|
424 YUI().use('handlebars-base', function (Y) { |
|
425 // Convert the precompiled template function into a renderable template |
|
426 // function. |
|
427 var template = Y.Handlebars.template(precompiledTemplate); |
|
428 |
|
429 // Render it. |
|
430 template({pie: 'Pumpkin'}); |
|
431 }); |
|
432 </script> |
|
433 |
|
434 @method template |
|
435 @param {Function} template Precompiled Handlebars template function. |
|
436 @return {Function} Compiled template function. |
|
437 */ |
|
438 |
|
439 // Alias for Y.Handlebars.template(), used by Y.Template. |
|
440 Handlebars.revive = Handlebars.template; |
|
441 |
|
442 // Make Y.Template.Handlebars an alias for Y.Handlebars. |
|
443 Y.namespace('Template').Handlebars = Handlebars; |
|
444 |
|
445 |
|
446 }, '3.10.3', {"requires": []}); |