在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:chrisyip/koa-pug开源软件地址:https://github.com/chrisyip/koa-pug开源编程语言:TypeScript 90.6%开源软件介绍:Koa-pugSupport Pug@3. How to usenpm install koa-pug --save const Koa = require('koa')
const path = require('path')
const Pug = require('koa-pug')
const app = new Koa()
const pug = new Pug({
viewPath: path.resolve(__dirname, './views'),
locals: { /* variables and helpers */ },
basedir: 'path/for/pug/extends',
helperPath: [
'path/to/pug/helpers',
{ random: 'path/to/lib/random.js' },
{ _: require('lodash') }
],
app: app // Binding `ctx.render()`, equals to pug.use(app)
})
pug.locals.someKey = 'some value'
app.use(async ctx => {
await ctx.render('index', locals, true)
}) For const koa = require('koa')
const Pug = require('koa-pug')
const app = koa()
const pug = new Pug({ app: app })
app.use(function * () {
yield this.render('index', locals, true)
}) Use const pug = new Pug({
viewPath: path.resolve(__dirname, './views'),
locals: { /* variables and helpers */ },
basedir: 'path/for/pug/extends',
helperPath: [
'path/to/pug/helpers',
{ random: 'path/to/lib/random.js' },
{ _: require('lodash') }
],
// Can work with / without Koa
// app: app
})
async function sendEmail(recipients, tplFile, locals) {
const body = await pug.render(tplFile, locals)
await send(recipients, body)
} Options
Content-Type
await ctx.render('index')
ctx.type = 'text/plain' Global HelpersBy setting
Defining a Helper// format-date.js, `koa-pug` will convert filename to camel case and use it as module name
module.exports = function (input) {
return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear()
} Equals to: // Becasue of there is a `moduleName`, `koa-pug` will use it as module name instead of filename
module.exports = {
moduleName: 'formatDate',
moduleBody (input) {
return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear()
}
} Use help in Pug: p= formatDate(new Date())
How |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论