/think-view

View for ThinkJS 3.x

Primary LanguageJavaScriptMIT LicenseMIT

think-view

Build Status Coverage Status npm

Install

npm install think-view

How to use

config file src/config/extend.js:

const view = require('think-view');
module.exports = [
  view
];

config view in src/config/adapter.js:

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

then can use some methods in controller

methods in controller

assign

assign variable to view

module.exports = class extends think.Controller {
  indexAction(){
    this.assign('title', 'ThinkJS Application');
    this.assign({ //assign multi variable
      title: 'thinkjs',
      name: 'thinkjs doc'
    })
  }
}

render

render file

module.exports = class extends think.Controller {
  async indexAction(){
    //render file index_index.html
    const content1 = await this.render();
    const content2 = await this.render('doc'); //render doc.html
    const content3 = await this.render('doc', 'ejs'); //change view render type to ejs
    const content4 = await this.render('doc', {type: 'ejs', xxx: 'yyy'}); //add other properties
  }
}

display

display view file

module.exports = class extends think.Controller {
  indexAction(){
    //render file index_index.html
    return this.display();
  }
}