UNPKG

twing

Version:

First-class Twig engine for Node.js

65 lines (64 loc) 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeWithNodeSynchronously = exports.executeWithNode = void 0; const context_1 = require("../context"); const runtime_1 = require("../error/runtime"); const merge_iterables_1 = require("../helpers/merge-iterables"); const iterator_to_map_1 = require("../helpers/iterator-to-map"); const is_plain_object_1 = require("../helpers/is-plain-object"); const executeWithNode = async (node, executionContext) => { const { template, nodeExecutor: execute, context } = executionContext; const { variables: variablesNode, body } = node.children; const { only } = node.attributes; let scopedContext; if (variablesNode) { const variables = await execute(variablesNode, executionContext); if (typeof variables !== "object") { throw (0, runtime_1.createRuntimeError)(`Variables passed to the "with" tag must be a hash.`, node, template.source); } if (only) { scopedContext = (0, context_1.createContext)(); } else { scopedContext = context.clone(); } scopedContext = (0, context_1.createContext)((0, merge_iterables_1.mergeIterables)(scopedContext, (0, iterator_to_map_1.iteratorToMap)(variables))); } else { scopedContext = context.clone(); } scopedContext.set('_parent', context.clone()); await execute(body, Object.assign(Object.assign({}, executionContext), { context: scopedContext })); }; exports.executeWithNode = executeWithNode; const executeWithNodeSynchronously = (node, executionContext) => { const { template, nodeExecutor: execute, context } = executionContext; const { variables: variablesNode, body } = node.children; const { only } = node.attributes; let scopedContext; if (variablesNode) { let variables = execute(variablesNode, executionContext); if ((0, is_plain_object_1.isPlainObject)(variables)) { variables = new Map(Object.entries(variables)); } if (typeof variables !== "object") { throw (0, runtime_1.createRuntimeError)(`Variables passed to the "with" tag must be a hash.`, node, template.source); } if (only) { scopedContext = new Map(); } else { scopedContext = new Map(context.entries()); } scopedContext = new Map([ ...scopedContext.entries(), ...variables.entries() ]); } else { scopedContext = new Map(context.entries()); } scopedContext.set('_parent', context); execute(body, Object.assign(Object.assign({}, executionContext), { context: scopedContext })); }; exports.executeWithNodeSynchronously = executeWithNodeSynchronously;