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/
8
YUI.add('handlebars-base', function (Y, NAME) {
11
Handlebars.js - Copyright (C) 2011 Yehuda Katz
12
https://raw.github.com/wycats/handlebars.js/master/LICENSE
14
// This file contains YUI-specific wrapper code and overrides for the
15
// handlebars-base module.
18
Handlebars is a simple template language inspired by Mustache.
20
This is a YUI port of the original Handlebars project, which can be found at
21
<https://github.com/wycats/handlebars.js>.
29
Provides basic Handlebars template rendering functionality. Use this module when
30
you only need to render pre-compiled templates.
33
@submodule handlebars-base
37
Handlebars is a simple template language inspired by Mustache.
39
This is a YUI port of the original Handlebars project, which can be found at
40
<https://github.com/wycats/handlebars.js>.
45
var Handlebars = Y.namespace('Handlebars');
46
/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
48
Handlebars.VERSION = "1.0.0-rc.4";
49
Handlebars.COMPILER_REVISION = 3;
51
Handlebars.REVISION_CHANGES = {
52
1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
57
Handlebars.helpers = {};
58
Handlebars.partials = {};
60
var toString = Object.prototype.toString,
61
functionType = '[object Function]',
62
objectType = '[object Object]';
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);
69
if (inverse) { fn.not = inverse; }
70
this.helpers[name] = fn;
74
Handlebars.registerPartial = function(name, str) {
75
if (toString.call(name) === objectType) {
76
Handlebars.Utils.extend(this.partials, name);
78
this.partials[name] = str;
82
Handlebars.registerHelper('helperMissing', function(arg) {
83
if(arguments.length === 2) {
86
throw new Error("Could not find property '" + arg + "'");
90
Handlebars.registerHelper('blockHelperMissing', function(context, options) {
91
var inverse = options.inverse || function() {}, fn = options.fn;
93
var type = toString.call(context);
95
if(type === functionType) { context = context.call(this); }
97
if(context === true) {
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);
105
return inverse(this);
112
Handlebars.K = function() {};
114
Handlebars.createFrame = Object.create || function(object) {
115
Handlebars.K.prototype = object;
116
var obj = new Handlebars.K();
117
Handlebars.K.prototype = null;
121
Handlebars.logger = {
122
DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
124
methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'},
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);
137
Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); };
139
Handlebars.registerHelper('each', function(context, options) {
140
var fn = options.fn, inverse = options.inverse;
141
var i = 0, ret = "", data;
144
data = Handlebars.createFrame(options.data);
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 });
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});
171
Handlebars.registerHelper('if', function(context, options) {
172
var type = toString.call(context);
173
if(type === functionType) { context = context.call(this); }
175
if(!context || Handlebars.Utils.isEmpty(context)) {
176
return options.inverse(this);
178
return options.fn(this);
182
Handlebars.registerHelper('unless', function(context, options) {
183
return Handlebars.helpers['if'].call(this, context, {fn: options.inverse, inverse: options.fn});
186
Handlebars.registerHelper('with', function(context, options) {
187
if (!Handlebars.Utils.isEmpty(context)) return options.fn(context);
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);
194
/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
196
var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
198
Handlebars.Exception = function(message) {
199
var tmp = Error.prototype.constructor.apply(this, arguments);
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]];
206
Handlebars.Exception.prototype = new Error();
208
// Build out our basic SafeString type
209
Handlebars.SafeString = function(string) {
210
this.string = string;
212
Handlebars.SafeString.prototype.toString = function() {
213
return this.string.toString();
225
var badChars = /[&<>"'`]/g;
226
var possible = /[&<>"'`]/;
228
var escapeChar = function(chr) {
229
return escape[chr] || "&";
233
extend: function(obj, value) {
234
for(var key in value) {
235
if(value.hasOwnProperty(key)) {
236
obj[key] = value[key];
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) {
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();
254
if(!possible.test(string)) { return string; }
255
return string.replace(badChars, escapeChar);
258
isEmpty: function(value) {
259
if (!value && value !== 0) {
261
} else if(toString.call(value) === "[object Array]" && value.length === 0) {
268
/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
271
template: function(templateSpec) {
274
escapeExpression: Handlebars.Utils.escapeExpression,
275
invokePartial: Handlebars.VM.invokePartial,
277
program: function(i, fn, data) {
278
var programWrapper = this.programs[i];
280
programWrapper = Handlebars.VM.program(i, fn, data);
281
} else if (!programWrapper) {
282
programWrapper = this.programs[i] = Handlebars.VM.program(i, fn);
284
return programWrapper;
286
programWithDepth: Handlebars.VM.programWithDepth,
287
noop: Handlebars.VM.noop,
291
return function(context, options) {
292
options = options || {};
293
var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
295
var compilerInfo = container.compilerInfo || [],
296
compilerRevision = compilerInfo[0] || 1,
297
currentRevision = Handlebars.COMPILER_REVISION;
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+").";
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]+").";
316
programWithDepth: function(i, fn, data /*, $depth */) {
317
var args = Array.prototype.slice.call(arguments, 3);
319
var program = function(context, options) {
320
options = options || {};
322
return fn.apply(this, [context, options.data || data].concat(args));
325
program.depth = args.length;
328
program: function(i, fn, data) {
329
var program = function(context, options) {
330
options = options || {};
332
return fn(context, options.data || data);
338
noop: function() { return ""; },
339
invokePartial: function(partial, name, context, helpers, partials, data) {
340
var options = { helpers: helpers, partials: partials, data: data };
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");
349
partials[name] = Handlebars.compile(partial, {data: data !== undefined});
350
return partials[name](context, options);
355
Handlebars.template = Handlebars.VM.template;
356
// This file contains YUI-specific wrapper code and overrides for the
357
// handlebars-base module.
359
Handlebars.VERSION += '-yui';
362
Registers a helper function that will be made available to all templates.
364
Helper functions receive the current template context as the `this` object, and
365
can also receive arguments passed by the template.
369
Y.Handlebars.registerHelper('linkify', function () {
370
return '<a href="' + Y.Escape.html(this.url) + '">' +
371
Y.Escape.html(this.text) + '</a>';
374
var source = '<ul>{{#links}}<li>{{{linkify}}}</li>{{/links}}</ul>';
376
Y.Handlebars.render(source, {
378
{url: '/foo', text: 'Foo'},
379
{url: '/bar', text: 'Bar'},
380
{url: '/baz', text: 'Baz'}
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.
393
Registers a partial that will be made available to all templates.
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.
400
Partials may be specified as uncompiled template strings or as compiled template
405
Y.Handlebars.registerPartial('header', '<h1>{{title}}</h1>');
406
Y.Handlebars.registerPartial('footer', 'Copyright (c) 2011 by Me.');
408
var source = '{{> header}} <p>Mustaches are awesome!</p> {{> footer}}';
410
Y.Handlebars.render(source, {title: 'My Page About Mustaches'});
412
@method registerPartial
413
@param {String} name Name of this partial.
414
@param {Function|String} partial Template string or compiled template function.
418
Converts a precompiled template into a renderable template function.
422
<script src="precompiled-template.js"></script>
424
YUI().use('handlebars-base', function (Y) {
425
// Convert the precompiled template function into a renderable template
427
var template = Y.Handlebars.template(precompiledTemplate);
430
template({pie: 'Pumpkin'});
435
@param {Function} template Precompiled Handlebars template function.
436
@return {Function} Compiled template function.
439
// Alias for Y.Handlebars.template(), used by Y.Template.
440
Handlebars.revive = Handlebars.template;
442
// Make Y.Template.Handlebars an alias for Y.Handlebars.
443
Y.namespace('Template').Handlebars = Handlebars;
446
}, '3.10.3', {"requires": []});