{"id":13527261,"url":"https://github.com/queckezz/koa-views","last_synced_at":"2025-05-14T18:05:48.553Z","repository":{"id":12648822,"uuid":"15320425","full_name":"queckezz/koa-views","owner":"queckezz","description":"Template rendering middleware for koa (hbs, swig, pug, anything! :sparkles:)","archived":false,"fork":false,"pushed_at":"2023-03-04T02:28:09.000Z","size":617,"stargazers_count":709,"open_issues_count":7,"forks_count":83,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-05-10T02:05:11.421Z","etag":null,"topics":["koa","template-engine"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/queckezz.png","metadata":{"files":{"readme":"README.md","changelog":"history.md","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}},"created_at":"2013-12-19T19:17:25.000Z","updated_at":"2025-05-03T04:55:29.000Z","dependencies_parsed_at":"2023-02-13T03:01:07.616Z","dependency_job_id":"e8c5e6c3-c598-42c6-a775-7756382f86ee","html_url":"https://github.com/queckezz/koa-views","commit_stats":{"total_commits":241,"total_committers":41,"mean_commits":5.878048780487805,"dds":"0.46887966804979253","last_synced_commit":"e7b8e4d50160a94ccc9eef2d4fe6488d297b5dde"},"previous_names":["queckezz/koa-render"],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queckezz%2Fkoa-views","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queckezz%2Fkoa-views/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queckezz%2Fkoa-views/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queckezz%2Fkoa-views/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/queckezz","download_url":"https://codeload.github.com/queckezz/koa-views/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253426183,"owners_count":21906500,"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":["koa","template-engine"],"created_at":"2024-08-01T06:01:44.354Z","updated_at":"2025-05-14T18:05:43.535Z","avatar_url":"https://github.com/queckezz.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","仓库","Node.js, koa"],"sub_categories":["中间件","Utilites"],"readme":"# koa-views\n\n![koa-views](https://img.shields.io/github/workflow/status/queckezz/koa-views/koa-views?logo=github\u0026style=flat-square)\n[![NPM version][npm-image]][npm-url]\n[![NPM downloads][npm-downloads-image]][npm-url]\n[![Dependency Status][david-image]][david-url]\n[![License][license-image]][license-url]\n\nTemplate rendering middleware for `koa@2`.\n\n## Installation\n\n```sh\nnpm install koa-views\n```\n\n## Templating engines\n\n`koa-views` is using [consolidate](https://github.com/tj/consolidate.js) under the hood.\n\n[List of supported engines](https://github.com/tj/consolidate.js#supported-template-engines)\n\n**NOTE**: you must still install the engines you wish to use, add them to your package.json dependencies.\n\n## Example\n\n```js\nvar views = require('koa-views');\n\nconst render = views(__dirname + '/views', {\n  map: {\n    html: 'underscore'\n  }\n})\n\n// Must be used before any router is used\napp.use(render)\n// OR Expand by app.context\n// No order restrictions\n// app.context.render = render()\n\napp.use(async function (ctx) {\n  ctx.state = {\n    session: this.session,\n    title: 'app'\n  };\n\n  await ctx.render('user', {\n    user: 'John'\n  });\n});\n```\n\nFor more examples you can take a look at the [tests](./test/index.js).\n\n## Simple middleware\n\nIf you need to simply render pages with locals, you can install `koa-views-render`:\n\n```sh\nnpm install koa-views-render\n```\n\nThen simply use it on your routes and its arguments will be passed to `ctx.render`.\n\n```js\nvar render = require('koa-views-render');\n\n// ...\n\napp.use(render('home', { title : 'Home Page' }));\n```\n\n## API\n\n#### `views(root, opts)`\n\n* `root`: Where your views are located. Must be an absolute path. All rendered views are relative to this path\n* `opts` (optional)\n\n* `opts.autoRender`: Whether to use `ctx.body` to receive the rendered template string. Defaults to `true`.\n\n```js\nconst render = views(__dirname, { autoRender: false, extension: 'pug' });\napp.use(render)\n// OR\n// app.context.render = render()\n\napp.use(async function (ctx) {\n  return await ctx.render('user.pug')\n})\n```\n\nvs.\n\n```js\nconst render = views(__dirname, { extension: 'pug' })\napp.use(render)\n// OR\n// app.context.render = render()\n\napp.use(async function (ctx) {\n  await ctx.render('user.pug')\n})\n```\n\n* `opts.extension`: Default extension for your views\n\nInstead of providing the full file extension you can omit it.\n```js\napp.use(async function (ctx) {\n  await ctx.render('user.pug')\n})\n```\n\nvs.\n\n```js\nconst render = views(__dirname, { extension: 'pug' })\napp.use(render)\n// OR\n// app.context.render = render()\n\napp.use(async function (ctx) {\n  await ctx.render('user')\n})\n```\n\n* `opts.map`: Map a file extension to an engine\n\nIn this example, each file ending with `.html` will get rendered using the `nunjucks` templating engine.\n```js\nconst render = views(__dirname, { map: {html: 'nunjucks' }})\napp.use(render)\n// OR\n// app.context.render = render()\n// render `user.html` with nunjucks\napp.use(async function (ctx) {\n  await ctx.render('user.html')\n})\n```\n\n* `opts.engineSource`: replace consolidate as default engine source\n\nIf you’re not happy with consolidate or want more control over the engines, you can override it with this options. `engineSource` should\nbe an object that maps an extension to a function that receives a path and options and returns a promise. In this example templates with the `foo` extension will always return `bar`.\n\n```js\nconst render = views(__dirname, { engineSource: {foo: () =\u003e Promise.resolve('bar')}})\napp.use(render)\n// OR\n// app.context.render = render()\n\napp.use(async function (ctx) {\n  await ctx.render('index.foo')\n})\n```\n\n* `opts.options`: These options will get passed to the view engine. This is the time to add `partials` and `helpers` etc.\n\n```js\nconst app = new Koa()\n  .use(views(__dirname, {\n    map: { hbs: 'handlebars' },\n    options: {\n      helpers: {\n        uppercase: (str) =\u003e str.toUpperCase()\n      },\n\n      partials: {\n        subTitle: './my-partial' // requires ./my-partial.hbs\n      },\n      \n      cache: true // cache the template string or not\n    }\n  }))\n  .use(function (ctx) {\n    ctx.state = { title: 'my title', author: 'queckezz' }\n    return ctx.render('./my-view.hbs')\n  })\n```\n\n## Debug\n\nSet the `DEBUG` environment variable to `koa-views` when starting your server.\n\n```bash\n$ DEBUG=koa-views\n```\n\n## License\n\n[MIT](./license)\n\n[npm-image]: https://img.shields.io/npm/v/koa-views.svg?style=flat-square\n[npm-downloads-image]: https://img.shields.io/npm/dm/koa-views.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-views\n[david-image]: http://img.shields.io/david/queckezz/koa-views.svg?style=flat-square\n[david-url]: https://david-dm.org/queckezz/koa-views\n[license-image]: http://img.shields.io/npm/l/koa-views.svg?style=flat-square\n[license-url]: ./license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqueckezz%2Fkoa-views","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqueckezz%2Fkoa-views","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqueckezz%2Fkoa-views/lists"}