diff options
Diffstat (limited to 'lib/handlebars/runtime.js')
-rw-r--r-- | lib/handlebars/runtime.js | 196 |
1 files changed, 104 insertions, 92 deletions
diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index 81e8aef..eeef182 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -1,6 +1,5 @@ -exports.attach = function(Handlebars) { - -// BEGIN(BROWSER) +import { escapeExpression, extend, Exception } from "./utils"; +import { COMPILER_REVISION, REVISION_CHANGES } from "./base"; function checkRevision(compilerInfo) { var compilerRevision = compilerInfo && compilerInfo[0] || 1, @@ -20,105 +19,118 @@ function checkRevision(compilerInfo) { } } -Handlebars.VM = { - template: function(templateSpec) { - // Just add water - var container = { - escapeExpression: Handlebars.Utils.escapeExpression, - invokePartial: Handlebars.VM.invokePartial, - programs: [], - program: function(i, fn, data) { - var programWrapper = this.programs[i]; - if(data) { - programWrapper = Handlebars.VM.program(i, fn, data); - } else if (!programWrapper) { - programWrapper = this.programs[i] = Handlebars.VM.program(i, fn); - } - return programWrapper; - }, - merge: function(param, common) { - var ret = param || common; - - if (param && common && (param !== common)) { - ret = {}; - Handlebars.Utils.extend(ret, common); - Handlebars.Utils.extend(ret, param); - } - return ret; - }, - programWithDepth: Handlebars.VM.programWithDepth, - noop: Handlebars.VM.noop, - compilerInfo: null +// TODO: Remove this line and break up compilePartial + +export function template(templateSpec, Hbars, compile) { + if (compile) { + var invokePartialWrapper = function(partial, name, context, helpers, partials, data) { + // TODO : Check this for all inputs and the options handling (partial flag, etc). This feels + // like there should be a common exec path + var result = invokePartial.apply(this, arguments); + if (result) { return result; } + + var options = { helpers: helpers, partials: partials, data: data }; + partials[name] = compile(partial, { data: data !== undefined }); + return partials[name](context, options); }; + } else { + var invokePartialWrapper = function(partial, name, context, helpers, partials, data) { + var result = invokePartial.apply(this, arguments); + if (result) { return result; } + throw new Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); + }; + } - return function(context, options) { - options = options || {}; - var namespace = options.partial ? options : Handlebars, - helpers, - partials; + if (!Hbars) { + throw new Error("YUNO HANDLEBARS"); + } - if (!options.partial) { - helpers = options.helpers; - partials = options.partials; + // Just add water + var container = { + escapeExpression: escapeExpression, + invokePartial: invokePartialWrapper, + programs: [], + program: function(i, fn, data) { + var programWrapper = this.programs[i]; + if(data) { + programWrapper = program(i, fn, data); + } else if (!programWrapper) { + programWrapper = this.programs[i] = program(i, fn); } - var result = templateSpec.call( - container, - namespace, context, - helpers, - partials, - options.data); - - if (!options.partial) { - checkRevision(container.compilerInfo); + return programWrapper; + }, + merge: function(param, common) { + var ret = param || common; + + if (param && common && (param !== common)) { + ret = {}; + extend(ret, common); + extend(ret, param); } + return ret; + }, + programWithDepth: programWithDepth, + noop: noop, + compilerInfo: null + }; + + return function(context, options) { + options = options || {}; + var namespace = options.partial ? options : Handlebars, + helpers, + partials; + + if (!options.partial) { + helpers = options.helpers; + partials = options.partials; + } + var result = templateSpec.call( + container, + namespace, context, + helpers, + partials, + options.data); - return result; - }; - }, + if (!options.partial) { + checkRevision(container.compilerInfo); + } - programWithDepth: function(i, fn, data /*, $depth */) { - var args = Array.prototype.slice.call(arguments, 3); + return result; + }; +} - var program = function(context, options) { - options = options || {}; +export function programWithDepth(i, fn, data /*, $depth */) { + var args = Array.prototype.slice.call(arguments, 3); - return fn.apply(this, [context, options.data || data].concat(args)); - }; - program.program = i; - program.depth = args.length; - return program; - }, - program: function(i, fn, data) { - var program = function(context, options) { - options = options || {}; - - return fn(context, options.data || data); - }; - program.program = i; - program.depth = 0; - return program; - }, - noop: function() { return ""; }, - invokePartial: function(partial, name, context, helpers, partials, data) { - var options = { partial: true, helpers: helpers, partials: partials, data: data }; - - if(partial === undefined) { - throw new Handlebars.Exception("The partial " + name + " could not be found"); - } else if(partial instanceof Function) { - return partial(context, options); - } else if (!Handlebars.compile) { - throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); - } else { - partials[name] = Handlebars.compile(partial, {data: data !== undefined}); - return partials[name](context, options); - } - } -}; + var program = function(context, options) { + options = options || {}; -Handlebars.template = Handlebars.VM.template; + return fn.apply(this, [context, options.data || data].concat(args)); + }; + program.program = i; + program.depth = args.length; + return program; +} + +export function program(i, fn, data) { + var program = function(context, options) { + options = options || {}; + + return fn(context, options.data || data); + }; + program.program = i; + program.depth = 0; + return program; +} -// END(BROWSER) +export function invokePartial(partial, name, context, helpers, partials, data) { + var options = { partial: true, helpers: helpers, partials: partials, data: data }; -return Handlebars; + if(partial === undefined) { + throw new Exception("The partial " + name + " could not be found"); + } else if(partial instanceof Function) { + return partial(context, options); + } +} -}; +export function noop() { return ""; } |