{"id":13798917,"url":"https://github.com/chrisyip/koa-pug","last_synced_at":"2025-10-28T11:18:04.074Z","repository":{"id":17576563,"uuid":"20380008","full_name":"chrisyip/koa-pug","owner":"chrisyip","description":"A Pug middleware for Koa","archived":false,"fork":false,"pushed_at":"2024-02-05T12:57:51.000Z","size":699,"stargazers_count":107,"open_issues_count":12,"forks_count":17,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-02T11:02:40.654Z","etag":null,"topics":["javascript","koa","koa-pug","pug","pug-templates"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrisyip.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-06-01T15:29:13.000Z","updated_at":"2024-10-11T09:58:32.000Z","dependencies_parsed_at":"2024-02-05T14:06:39.550Z","dependency_job_id":null,"html_url":"https://github.com/chrisyip/koa-pug","commit_stats":{"total_commits":84,"total_committers":13,"mean_commits":6.461538461538462,"dds":0.5119047619047619,"last_synced_commit":"b66af7c15d7ecb7619011bf1d4db30f73e147967"},"previous_names":["chrisyip/koa-jade"],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisyip%2Fkoa-pug","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisyip%2Fkoa-pug/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisyip%2Fkoa-pug/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisyip%2Fkoa-pug/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrisyip","download_url":"https://codeload.github.com/chrisyip/koa-pug/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252428013,"owners_count":21746319,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","koa","koa-pug","pug","pug-templates"],"created_at":"2024-08-04T00:00:56.592Z","updated_at":"2025-10-28T11:18:03.946Z","avatar_url":"https://github.com/chrisyip.png","language":"TypeScript","readme":"# Koa-pug\n\n[![Node version][node-image]][npm-url] [![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Travis CI][travis-image]][travis-url]\n\nA [Pug](https://github.com/pugjs) middleware for [Koa](http://koajs.com/).\n\nSupport Pug@3.\n\n# How to use\n\n```bash\nnpm install koa-pug --save\n```\n\n```js\nconst Koa = require('koa')\nconst path = require('path')\nconst Pug = require('koa-pug')\n\nconst app = new Koa()\nconst pug = new Pug({\n  viewPath: path.resolve(__dirname, './views'),\n  locals: { /* variables and helpers */ },\n  basedir: 'path/for/pug/extends',\n  helperPath: [\n    'path/to/pug/helpers',\n    { random: 'path/to/lib/random.js' },\n    { _: require('lodash') }\n  ],\n  app: app // Binding `ctx.render()`, equals to pug.use(app)\n})\n\npug.locals.someKey = 'some value'\n\napp.use(async ctx =\u003e {\n  await ctx.render('index', locals, true)\n})\n```\n\nFor `koa@1`:\n\n```js\nconst koa = require('koa')\nconst Pug = require('koa-pug')\n\nconst app = koa()\nconst pug = new Pug({ app: app })\n\napp.use(function * () {\n  yield this.render('index', locals, true)\n})\n```\n\nUse `koa-pug` as a standalone Pug renderer:\n\n```js\nconst pug = new Pug({\n  viewPath: path.resolve(__dirname, './views'),\n  locals: { /* variables and helpers */ },\n  basedir: 'path/for/pug/extends',\n  helperPath: [\n    'path/to/pug/helpers',\n    { random: 'path/to/lib/random.js' },\n    { _: require('lodash') }\n  ],\n  // Can work with / without Koa\n  // app: app\n})\n\nasync function sendEmail(recipients, tplFile, locals) {\n  const body = await pug.render(tplFile, locals)\n  await send(recipients, body)\n}\n```\n\n## Options\n\n`options` are extended from [Pug's options](https://pugjs.org/api/reference.html#options), all options will be passed to Pug compiler except the following:\n\n`viewPath: string`: the location of Pug templates. Default is `process.cwd()`.\n\n`locals: { [key: string]: any }`: variables and helpers that will be passed to Pug compiler.\n\n`helperPath: string | string[] | { [key string]: string }`: location(s) of helper(s).\n\n## Content-Type\n\n`koa-pug` will set `content-type` to `text/html` for you, you can change it:\n\n```js\nawait ctx.render('index')\nctx.type = 'text/plain'\n```\n\n## Global Helpers\n\nBy setting `helperPath`, koa-pug will load all the modules that under sepecified folxder, and make them available on all templates.\n\n`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')`\n\n### Defining a Helper\n\n```js\n// format-date.js, `koa-pug` will convert filename to camel case and use it as module name\nmodule.exports = function (input) {\n  return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear()\n}\n```\n\nEquals to:\n\n```js\n// Becasue of there is a `moduleName`, `koa-pug` will use it as module name instead of filename\nmodule.exports = {\n  moduleName: 'formatDate',\n  moduleBody (input) {\n    return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear()\n  }\n}\n```\n\nUse help in Pug:\n\n```pug\np= formatDate(new Date())\n```\n\n# How `koa-pug` resolves Pug template files\n\nLet's say the project views structure like:\n\n```\nviews\n├── user.pug\n├── user\n│   └── index.pug\n└── file\n    └── index.pug\n```\n\n`koa-pug` will search file in the following order:\n\n- `\u003ctpl_name\u003e.pug`\n- `\u003ctpl_name\u003e/index.pug`\n\nWhen `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)`.\n\nWhen `pug.render('file')` is called, `views/file/index.pug` will be rendered.\n\n# Contributors\n\nVia [GitHub](https://github.com/chrisyip/koa-pug/graphs/contributors)\n\n[node-image]: https://img.shields.io/node/v/koa-pug.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-pug\n[npm-image]: https://img.shields.io/npm/v/koa-pug.svg?style=flat-square\n[daviddm-url]: https://david-dm.org/chrisyip/koa-pug\n[daviddm-image]: https://img.shields.io/david/chrisyip/koa-pug.svg?style=flat-square\n[travis-url]: https://travis-ci.org/chrisyip/koa-pug\n[travis-image]: https://img.shields.io/travis/chrisyip/koa-pug.svg?style=flat-square\n","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisyip%2Fkoa-pug","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrisyip%2Fkoa-pug","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisyip%2Fkoa-pug/lists"}