Skip to content

Instantly share code, notes, and snippets.

@ThaddeusJiang
Last active August 8, 2018 00:18
Show Gist options
  • Save ThaddeusJiang/45e5d395f4de44599975e2ea61beb558 to your computer and use it in GitHub Desktop.
Save ThaddeusJiang/45e5d395f4de44599975e2ea61beb558 to your computer and use it in GitHub Desktop.
koa2 html template xtemplate 示例
  1. Install
yarn add xtemplate xtpl
  1. 结合 koa2 see server.js

  2. 定义 html 模版 see jwt.xtpl

<!- /views/jwt.xtpl -->
<h1>{{title}}</h1>
const Koa = require('koa')
const Router = require('koa-router')
// 1.
const xtpl = require('xtpl/lib/koa2')
const app = new Koa()
const router = new Router()
// 2.
xtpl(app, {
views: './views',
})
// 3.
router.get('/', async (ctx, next) => {
ctx.body = await ctx.render('jwt', { title: 'hello, xtemplate' })
})
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(3000)
console.log('open: http://localhost:3000')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment