/think-view-nunjucks

Use nunjucks to render view files for ThinkJS 3.x

Primary LanguageJavaScriptMIT LicenseMIT

think-view-nunjucks

Build Status Coverage Status npm

Install

npm install think-view-nunjucks

How to Usage

edit config file src/config/adapter.js, add options:

const nunjucks = require('think-view-nunjucks');
exports.view = {
  type: 'nunjucks',
  common: {
    viewPath: path.join(think.ROOT_PATH, 'view'),
    extname: '.html',
    sep: '_' //seperator between controller and action
  },
  nunjucks: {
    handle: nunjucks,
    beforeRender: (env, nunjucks, config) => {}
  }
}

default options

const defaultOptions = {
  autoescape: true,
  watch: false,
  noCache: false,
  throwOnUndefined: false
};

change options:

exports.view = {
  type: 'nunjucks',
  nunjucks: {
    handle: nunjucks,
    options: {
      tags: {
        blockStart: '<%',
        blockEnd: '%>',
        variableStart: '<$',
        variableEnd: '$>',
        commentStart: '<#',
        commentEnd: '#>'
      }
    },
    beforeRender: (env, nunjucks, handleOptions) => {}
  }
}

you can find all nunjucks support options at https://mozilla.github.io/nunjucks/api.html#configure

beforeRender

you can use beforeRender method to set some env:

exports.view = {
  type: 'nunjucks',
  nunjucks: {
    handle: nunjucks,
    beforeRender: (env, nunjucks, handleOptions) => {
      env.addGlobal('think', think);
      env.addGlobal('JSON', JSON);
    }
  }
}

you can find all APIs in env at https://mozilla.github.io/nunjucks/api.html#environment