Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
npm 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 koa@1
:
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 koa-pug
as a standalone Pug renderer:
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
are extended from Pug's options, all options will be passed to Pug compiler except the following:
viewPath: string
: the location of Pug templates. Default is process.cwd()
.
locals: { [key: string]: any }
: variables and helpers that will be passed to Pug compiler.
helperPath: string | string[] | { [key string]: string }
: location(s) of helper(s).
koa-pug
will set content-type
to text/html
for you, you can change it:
await ctx.render('index')
ctx.type = 'text/plain'
By setting helperPath
, koa-pug will load all the modules that under sepecified folxder, and make them available on all templates.
helperPath
also could be an array including folders, files path, even moduleName: 'path/to/lib.js
mapping object. Also support node module as a helper, just like: '_': require('lodash')
// 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())
koa-pug
resolves Pug template filesLet's say the project views structure like:
views
├── user.pug
├── user
│ └── index.pug
└── file
└── index.pug
koa-pug
will search file in the following order:
<tpl_name>.pug
<tpl_name>/index.pug
When pug.render('user')
is called, views/user.pug
will be rendered. If you want to render views/user/index.pug
, you have to pass it to renderer explicitly: pug.render('user/index)
.
When pug.render('file')
is called, views/file/index.pug
will be rendered.
Via GitHub
FAQs
A Pug middleware for Koa
The npm package koa-pug receives a total of 11,736 weekly downloads. As such, koa-pug popularity was classified as popular.
We found that koa-pug demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.