UNPKG

@midwayjs/view-nunjucks

Version:

Midway Component for nunjucks render

87 lines 3.79 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NunjucksEnvironment = void 0; const core_1 = require("@midwayjs/core"); const nunjucks_1 = require("nunjucks"); class MidwayNunjucksEnvironment extends nunjucks_1.Environment { constructor(fileLoader, config) { super(fileLoader, config); // http://disse.cting.org/2016/08/02/2016-08-02-sandbox-break-out-nunjucks-template-engine const originMemberLookup = nunjucks_1.runtime.memberLookup; nunjucks_1.runtime.memberLookup = function (...args) { const val = args[1]; if (val === 'prototype' || val === 'constructor') return null; return originMemberLookup(...args); }; } } let NunjucksEnvironment = class NunjucksEnvironment { async init() { const coreLogger = this.app.getCoreLogger(); const viewPaths = this.globalConfig.view.root; coreLogger.info('[midway:view-nunjucks] loading templates from %j', viewPaths); const config = this.globalConfig['nunjucks']; config.noCache = !config.cache; delete config.cache; const nunjucksConfig = { noCache: config.noCache, throwOnUndefined: config.throwOnUndefined, trimBlocks: config.trimBlocks, lstripBlocks: config.lstripBlocks, tags: config.tags, autoescape: config.autoescape, }; const fileLoader = new nunjucks_1.FileSystemLoader(this.globalConfig.view.root, { noCache: nunjucksConfig.noCache, }); this.nunjucksEnvironment = new MidwayNunjucksEnvironment(fileLoader, nunjucksConfig); } render(name, context, callback) { return this.nunjucksEnvironment.render(name, context, callback); } renderString(tpl, context, options, callback) { return this.nunjucksEnvironment.renderString(tpl, context, options, callback); } addFilter(name, callback) { return this.nunjucksEnvironment.addFilter(name, callback); } getFilter(name) { return this.nunjucksEnvironment.getFilter(name); } hasExtension(name) { return this.nunjucksEnvironment.hasExtension(name); } addGlobal(name, value) { return this.nunjucksEnvironment.addGlobal(name, value); } }; __decorate([ (0, core_1.App)(), __metadata("design:type", Object) ], NunjucksEnvironment.prototype, "app", void 0); __decorate([ (0, core_1.Config)(core_1.ALL), __metadata("design:type", Object) ], NunjucksEnvironment.prototype, "globalConfig", void 0); __decorate([ (0, core_1.Init)(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], NunjucksEnvironment.prototype, "init", null); NunjucksEnvironment = __decorate([ (0, core_1.Provide)(), (0, core_1.Scope)(core_1.ScopeEnum.Singleton) ], NunjucksEnvironment); exports.NunjucksEnvironment = NunjucksEnvironment; //# sourceMappingURL=engine.js.map