function initView(app) { const configService: ConfigService = app.get(ConfigService); const viewPath = path.join(__dirname, '../views'); app.setBaseViewsDir(viewPath); app.setViewEngine('njk'); const nunjucksEnv = nunjucks.configure(viewPath, { noCache: process.env.NODE_ENV === configService.DEVELOPMENT, autoescape: true, express: app, }); for (const key of Object.keys(viewfilter)) { nunjucksEnv.addFilter(key, viewfilter[key]); } // macro中不能访问当前 context , 将要访问的变量加到 global nunjucksEnv.addGlobal('env', configService.env); nunjucksEnv.addGlobal('jsPath', configService.static.jsPath); }
function templating(path, opts) { var env = createEnv(path, opts); return async (ctx, next) => { ctx.render = function (view, model) { ctx.response.body = env.render(view, Object.assign({}, ctx.state || {}, model || {})); ctx.response.type = 'text/html'; }; await next(); }; }
/** * Using WebLoader for load path view engine * @param views [Array] - Array path loader * @returns {*} */ static webLoader(views) { this.env = new nunjucks.Environment(new nunjucks.WebLoader(views)); return this.env; }
views() { nunjucks.configure(path.resolve(__dirname, 'app', 'views'), { watch: this.isDev, express: this.express, autoescape: true, }); this.express.use(express.static(path.resolve(__dirname, 'public'))); this.express.set('view engine', 'njk'); }
/** * Custom settings nunjucks template engine * @param setting [Object] - Nunjucks configure settings * @param opts [Object] - Nunjucks options setting for custom filter loader, show configure. */ configure(setting, opts) { if (!setting) setting = {}; this.app.set('view engine', 'crab'); let env = nunjucks.configure(setting.path, { express: this.app, autoescape: setting.autoescape || true, // default trimBlocks: setting.trimBlocks || true, lstripBlocks: setting.lstripBlocks || true, dev: setting.dev || false, noCache: setting.noCache || false, // default watch: setting.watch || false // default }); if (opts) { if (opts.showConfigure) Nunjucks.showConfigure(env); opts.filter ? Nunjucks.require(env, opts.filter) : Nunjucks.require(env, __base + '/customs/filters/*.js'); opts.addGlobal ? Nunjucks.require(env, opts.addGlobal) : Nunjucks.require(env, __base + '/customs/global/*.js'); Nunjucks.require(env, __base + '/core/nunjucks_global/*.js'); Nunjucks.require(env, __base + '/core/nunjucks_filter/*.js'); } return env; }
function createEnv(path, opts) { var autoescape = opts.autoescape === undefined ? true : opts.autoescape, noCache = opts.noCache || false, watch = opts.watch || false, throwOnUndefined = opts.throwOnUndefined || false, env = new nunjucks.Environment( new nunjucks.FileSystemLoader(path, { noCache: noCache, watch: watch, }), { autoescape: autoescape, throwOnUndefined: throwOnUndefined }); if (opts.filters) { for (var f in opts.filters) { env.addFilter(f, opts.filters[f]); } } return env; }
let __html = nunjucks.renderString(__file.toString(), { rootUrl: __rootUrl.join('') })
/** * Using FileSystemLoader for load path view engine * @param views [Array] - Array path loader * @returns {*} */ static systemLoader(views) { this.env = new nunjucks.Environment(new nunjucks.FileSystemLoader(views)); return this.env; }
function templating(path, opts) { // 创建Nunjucks的env对象: var env = createEnv(path, opts); return async (ctx, next) => { // 给ctx绑定render函数: ctx.render = function (view, model) { // 把render后的内容赋值给response.body: ctx.response.body = env.render(view, Object.assign({}, ctx.state || {}, model || {})); // 设置Content-Type: ctx.response.type = 'text/html'; }; // 继续处理请求: await next(); }; }
function templating(path, opts) { // 创建Nunjucks的env对象: var env = createEnv(path, opts); return async (ctx, next) => { // 给ctx绑定render函数: ctx.render = function (view, model) { // 把render后的内容赋值给response.body: ctx.response.body = env.render(view, Object.assign({}, ctx.state || {}, model || {})); // 设置Content-Type: ctx.response.type = 'text/html'; }; // 继续处理请求: await next(); }; }
function createEnv(path, opts) { var autoescape = opts.autoescape === undefined ? true : opts.autoescape, noCache = opts.noCache || false, watch = opts.watch || false, throwOnUndefined = opts.throwOnUndefined || false, env = new nunjucks.Environment( new nunjucks.FileSystemLoader(path, { noCache: noCache, watch: watch, }), { autoescape: autoescape, throwOnUndefined: throwOnUndefined }); if (opts.filters) { for (var f in opts.filters) { env.addFilter(f, opts.filters[f]); } } return env; }
// 模板引擎对象 function createEnv(path, opts) { var // 给每个参数加上默认值 autoescape = opts.autoescape === undefined ? true : opts.autoescape, noCache = opts.noCache || false, watch = opts.watch || false, throwOnUndefined = opts.throwOnUndefined || false, // 创建一个文件系统加载器 env = new nunjucks.Environment( // 从views目录读取模板 new nunjucks.FileSystemLoader(path, { noCache: noCache, watch: watch, }),{ autoescape: autoescape, throwOnUndefined: throwOnUndefined }); if (opts.filters) { for (var f in opts.filters) { env.addFilter(f, opts.filters[f]); } } return env; }
function createEnv(path, opts) { var autoescape = opts.autoescape === undefined ? true : opts.autoescape, noCache = opts.noCache || false, watch = opts.watch || false, throwOnUndefined = opts.throwOnUndefined || false, env = new nunjucks.Environment( new nunjucks.FileSystemLoader(path, { noCache: noCache, watch: watch, }), { autoescape: autoescape, throwOnUndefined: throwOnUndefined }); if (opts.filters) { for (var f in opts.filters) { env.addFilter(f, opts.filters[f]); } } return env; }
function createEnv(path, opts) { var autoescape = opts.autoescape === undefined ? true : opts.autoescape, noCache = opts.noCache || false, watch = opts.watch || false, throwOnUndefined = opts.throwOnUndefined || false, env = new nunjucks.Environment( new nunjucks.FileSystemLoader(path || 'views', { noCache: noCache, watch: watch, }), { autoescape: autoescape, throwOnUndefined: throwOnUndefined }); if (opts.filters) { for (var f in opts.filters) { env.addFilter(f, opts.filters[f]); } } return env; }
// 模板引擎对象 function createEnv(path, opts) { var // 给每个参数加上默认值 autoescape = opts.autoescape === undefined ? true : opts.autoescape, noCache = opts.noCache || false, watch = opts.watch || false, throwOnUndefined = opts.throwOnUndefined || false, // 创建一个文件系统加载器 env = new nunjucks.Environment( // 从views目录读取模板 new nunjucks.FileSystemLoader(path, { noCache: noCache, watch: watch, }),{ autoescape: autoescape, throwOnUndefined: throwOnUndefined }); if (opts.filters) { for (var f in opts.filters) { env.addFilter(f, opts.filters[f]); } } return env; }