项目作者: arshtepe

项目描述 :
Koa2 template render middleware
高级语言: JavaScript
项目地址: git://github.com/arshtepe/koa-render-view.git
创建时间: 2017-05-15T19:00:43Z
项目社区:https://github.com/arshtepe/koa-render-view

开源协议:MIT License

下载


koa-render-view

Koa2 template render middleware

Installation

  1. $ npm install --save koa-render-view

Examples

Basic usage

  1. const views = require('koa-render-view');
  2. const path = require('path');
  3. app.use(views(path.join(__dirname, '/views')));
  4. app.use(async function ({render}, next) {
  5. await render('index', {
  6. message: "Hello world"
  7. });
  8. });

React server side rendering example

  1. $ npm install --save react react-dom babel-preset-react

test.jsx

  1. const React = require('react');
  2. class Test extends React.Component {
  3. render() {
  4. return (
  5. <p>
  6. {this.props.message}
  7. </p>
  8. );
  9. }
  10. }
  11. module.exports = Test;
  1. app.use(views(path.join(__dirname, '/views')));
  2. app.use(async function ({render}, next) {
  3. await render('test.jsx', {
  4. message: "Hello world"
  5. });
  6. });

Parameters usage

  1. app.use(views(path.join(__dirname, '/views'), {
  2. recursive: true,
  3. map: {
  4. html: "ejs"
  5. }
  6. }));
  7. app.use(async function ({render}, next) {
  8. await render('index', {
  9. message: "Hello world"
  10. });
  11. });

Aliases usage

  1. const views = require('koa-render-view');
  2. const data = {
  3. message: "Hello world"
  4. };
  5. app.use(views(path.join(__dirname, '/views'), {
  6. aliases: [{
  7. alias: "@alias", //alias it is any string, but alias cann`t contain symbols from path \/.
  8. path: path.join(__dirname, "viewXXX")
  9. }]
  10. }));
  11. app.use(async function ({render}, next) {
  12. await render('index', data); //render index.html from views
  13. });
  14. app.use(async function ({render}, next) {
  15. await render('@alias/index', data);//render index.html from alias path ( "viewXXX" )
  16. });

Multiple view folders

If folders “views” and “views1” contain the same file, will be conflict and render any from the same file.
For example:

  1. const views = require('koa-render-view');
  2. const path = require('path');
  3. /*
  4. views
  5. -index.html
  6. views1
  7. -index.html
  8. */
  9. app.use(views([
  10. path.join(__dirname, '/views'),
  11. path.join(__dirname, '/views1')
  12. ]));
  13. app.use(async function ({render}, next) {
  14. await render('index', data);
  15. });

In this case midleware renders first index.html in its file list. To decide this you need to write part of path

  1. await render('views/index', data);// render index.html from views
  2. await render('views1/index', data);// render index.html from views1

API

views( {(string|string[])}, parameters )

By default use consolidate

[parameters.engine=consolidate]

Template engine.
Custom engine example:

  1. const engine = {
  2. html: (filePath, data) => {
  3. return "...";
  4. }
  5. app.use(views(path.join(__dirname, '/views'), {engine}))

[parameters.cache=true]

If cache true builds list of files when you create view middleware

  1. fs.writeFileSync("views/x.html", MESSAGE);
  2. app.use(views(path.join(__dirname, '/views')))
  3. app.use(async function ({render}, next) {
  4. await render('x.html', data); //thats work
  5. });

But in this case, it doesn`t work, you need to set cache: false

  1. app.use(views(path.join(__dirname, '/views')))
  2. app.use(async function ({render}, next) {
  3. fs.writeFileSync("views/x.html", MESSAGE);
  4. await render('x.html', data); //error
  5. });

[parameters.extension=”html”]

Default file extension, if extension === “html”,then render(“index”) equals render(“index.html”)

[parameters.recursive=false]

Recursive read directory

[parameters.map={}]

It is map extenstion to render engine

  1. app.use(views(path.join(__dirname, '/views'), {
  2. xejs: "ejs" // render file with extension .xejs by ejs engine
  3. }))

[parameters.aliases=[]]

You can create alias for path to your views

  • alias - must be a string starts from @,#,!,$,_ symbols and doesn`t contain symbols /\.
  • path - absolute path to directory
    ```js
    app.use(views(path.join(__dirname, ‘/views’), {
    aliases: [{
    1. alias: "@alias",
    2. path: path.join(__dirname, "viewXXX")
    }]
    }));

await render(‘index’, data); //render views/index.html
await render(‘@alias/index’, data); //render viewXXX/index.html
await render(‘@alias/deep/index’, data); //render viewXXX/deep/index.html
```

[parameters.options={}]

Render options field, these options will pass to render engine as parameters

License

MIT License

Copyright (c) 2017 Artem Shtepenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.