Skip to content
View for ThinkJS 3.x
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.eslintrc
.gitignore
.travis.yml
LICENSE
README.md
index.d.ts
index.js
package.json

README.md

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();
  }
}
You can’t perform that action at this time.