5.1.0 • Published 8 months ago

@zenweb/template v5.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

template - 服务端模版渲染

功能说明

在服务器端使用模版引擎渲染模版并根据请求条件按需输出

演示

安装

yarn add @zenweb/template @zenweb/template-nunjucks

配置

import { create } from 'zenweb';
import modTemplate from '@zenweb/template';
import nunjucks from '@zenweb/template-nunjucks';

create()

.setup(modTemplate({
  matchPath: [/\.html$/i],
  engine: nunjucks({
    filter: require('./template/filter'),
    global: require('./template/global'),
  }),
}))

.start();

控制器使用

import { Context, mapping, Body, $body } from 'zenweb';

export class Controller {
  @mapping({ path: '/', method: 'POST' })
  post(body: Body) {
    console.log(body.type); // POST body 内容类型
    console.log(body.data); // POST Body 内容解析完成后的数据
  }

  @mapping({ path: '/', method: 'POST' })
  async post() {
    console.log(await getAge()); // 类型转换&校验
  }
}

async function getAge() {
  return (await $body.get({ age: '!int' })).age;
}

模版代码

template/index.html

<h1>Hello {{name}}</h1>

依赖模块

  • @zenweb/result

Core 挂载项

Context 挂载项

  • templateOption
  • template(option) 设置当前请求的模版渲染选项

全局模式

方法功能
$template()设置当前请求的模版渲染选项

目前已支持的模版引擎

nunjucks handlebars

5.1.0

8 months ago

5.0.0

9 months ago

4.1.0

10 months ago

4.0.0

1 year ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago