diff options
Diffstat (limited to 'lib/handlebars/runtime.js')
-rw-r--r-- | lib/handlebars/runtime.js | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index 2585ef3..991bdfd 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -1,14 +1,33 @@ -import { escapeExpression, extend, Exceptions } from "handlebars/utils"; -import { COMPILER_REVISION, REVISION_CHANGES } from "handlebars/base"; +import { escapeExpression, extend, Exception } from "./utils"; +import { COMPILER_REVISION, REVISION_CHANGES } from "./base"; -// TODO: Deal with the fact that compile is necessary for on-the-fly partial -// compilation but won't be present for precompiled templates. +// TODO: Remove this line and break up compilePartial + +export function template(templateSpec, Hbars) { + // TODO: Make this less global + Hbars = Hbars || Handlebars; + + if (Hbars.compile) { + var invokePartialWrapper = function(partial, name, context, helpers, partials, data) { + var result = invokePartial.apply(this, arguments); + if (result) { return result; } + + var options = { helpers: helpers, partials: partials, data: data }; + partials[name] = Hbars.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"); + }; + } -export function template(templateSpec) { // Just add water var container = { escapeExpression: escapeExpression, - invokePartial: invokePartial, + invokePartial: invokePartialWrapper, programs: [], program: function(i, fn, data) { var programWrapper = this.programs[i]; @@ -36,8 +55,11 @@ export function template(templateSpec) { return function(context, options) { options = options || {}; + + Hbars = Hbars || require("handlebars"); + // TODO: Why does templateSpec require a reference to the global Handlebars? - var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); + var result = templateSpec.call(container, Hbars, context, options.helpers, options.partials, options.data); var compilerInfo = container.compilerInfo || [], compilerRevision = compilerInfo[0] || 1, @@ -84,8 +106,6 @@ export function program(i, fn, data) { return program; } -export function noop() { return ""; } - export function invokePartial(partial, name, context, helpers, partials, data) { var options = { helpers: helpers, partials: partials, data: data }; @@ -93,10 +113,7 @@ export function invokePartial(partial, name, context, helpers, partials, data) { throw new Exception("The partial " + name + " could not be found"); } else if(partial instanceof Function) { return partial(context, options); - } else if (!compile) { - throw new Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); - } else { - partials[name] = compile(partial, {data: data !== undefined}); - return partials[name](context, options); } } + +export function noop() { return ""; } |