MCPcopy
hub / github.com/handlebars-lang/handlebars.js / template

Function template

lib/handlebars/runtime.js:49–306  ·  view source on GitHub ↗
(templateSpec, env)

Source from the content-addressed store, hash-verified

47}
48
49export function template(templateSpec, env) {
50 /* istanbul ignore next */
51 if (!env) {
52 throw new Exception('No environment passed to template');
53 }
54 if (!templateSpec || !templateSpec.main) {
55 throw new Exception('Unknown template object: ' + typeof templateSpec);
56 }
57
58 templateSpec.main.decorator = templateSpec.main_d;
59
60 // Note: Using env.VM references rather than local var references throughout this section to allow
61 // for external users to override these as pseudo-supported APIs.
62 env.VM.checkRevision(templateSpec.compiler);
63
64 // backwards compatibility for precompiled templates with compiler-version 7 (<4.3.0)
65 const templateWasPrecompiledWithCompilerV7 =
66 templateSpec.compiler && templateSpec.compiler[0] === 7;
67
68 function invokePartialWrapper(partial, context, options) {
69 if (options.hash) {
70 context = Utils.extend({}, context, options.hash);
71 if (options.ids) {
72 options.ids[0] = true;
73 }
74 }
75 partial = env.VM.resolvePartial.call(this, partial, context, options);
76
77 options.hooks = this.hooks;
78 options.protoAccessControl = this.protoAccessControl;
79
80 let result = env.VM.invokePartial.call(this, partial, context, options);
81
82 if (result == null && env.compile) {
83 options.partials[options.name] = env.compile(
84 partial,
85 templateSpec.compilerOptions,
86 env
87 );
88 result = options.partials[options.name](context, options);
89 }
90 if (result != null) {
91 if (options.indent) {
92 let lines = result.split('\n');
93 for (let i = 0, l = lines.length; i < l; i++) {
94 if (!lines[i] && i + 1 === l) {
95 break;
96 }
97
98 lines[i] = options.indent + lines[i];
99 }
100 result = lines.join('\n');
101 }
102 return result;
103 } else {
104 throw new Exception(
105 'The partial ' +
106 options.name +

Calls 5

resultIsAllowedFunction · 0.90
createProtoAccessControlFunction · 0.90
moveHelperToHooksFunction · 0.90
wrapProgramFunction · 0.85
addHelpersFunction · 0.85

Tested by

no test coverage detected