{"id":14989589,"url":"https://github.com/arshtepe/koa-render-view","last_synced_at":"2025-04-12T01:40:49.387Z","repository":{"id":57289429,"uuid":"91374082","full_name":"arshtepe/koa-render-view","owner":"arshtepe","description":"Koa2 template render middleware","archived":false,"fork":false,"pushed_at":"2018-02-27T21:46:52.000Z","size":15,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T22:39:03.436Z","etag":null,"topics":["koa-middleware","koa2","react-server-render","view"],"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/arshtepe.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}},"created_at":"2017-05-15T19:00:43.000Z","updated_at":"2017-11-17T17:03:47.000Z","dependencies_parsed_at":"2022-08-29T16:01:43.001Z","dependency_job_id":null,"html_url":"https://github.com/arshtepe/koa-render-view","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arshtepe%2Fkoa-render-view","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arshtepe%2Fkoa-render-view/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arshtepe%2Fkoa-render-view/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arshtepe%2Fkoa-render-view/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arshtepe","download_url":"https://codeload.github.com/arshtepe/koa-render-view/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505740,"owners_count":21115352,"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-middleware","koa2","react-server-render","view"],"created_at":"2024-09-24T14:18:36.875Z","updated_at":"2025-04-12T01:40:49.369Z","avatar_url":"https://github.com/arshtepe.png","language":"JavaScript","readme":"# koa-render-view\n  Koa2 template render middleware\n\n## Installation\n```\n  $ npm install --save koa-render-view\n```  \n## Examples\n\n### Basic usage\n```javascript\nconst views = require('koa-render-view');\nconst path = require('path');\n\napp.use(views(path.join(__dirname, '/views')));\napp.use(async function ({render}, next) {\n  await render('index', {\n    message: \"Hello world\"\n  });\n});\n\n```\n\n### React server side rendering example\n```\n  $ npm install --save react react-dom babel-preset-react\n``` \n\ntest.jsx\n```js\nconst React = require('react');\n\nclass Test extends React.Component {\n    render() {\n        return (\n            \u003cp\u003e\n                {this.props.message}\n            \u003c/p\u003e\n        );\n    }\n}\n\nmodule.exports = Test;\n```\n\n```js\napp.use(views(path.join(__dirname, '/views')));\napp.use(async function ({render}, next) {\n  await render('test.jsx', {\n    message: \"Hello world\"\n  });\n});\n\n```\n\n### Parameters usage\n```javascript\napp.use(views(path.join(__dirname, '/views'), {\n      recursive: true,\n      map: {\n        html: \"ejs\"\n      }\n}));\napp.use(async function ({render}, next) {\n  await render('index', {\n    message: \"Hello world\"\n  });\n});\n\n```\n\n#### Aliases usage\n\n```javascript\nconst views = require('koa-render-view');\nconst data = {\n    message: \"Hello world\"\n};\n\napp.use(views(path.join(__dirname, '/views'), {\n  aliases: [{\n      alias: \"@alias\", //alias it is any string, but alias cann`t contain symbols from path \\/. \n      path: path.join(__dirname, \"viewXXX\")\n  }]\n}));\napp.use(async function ({render}, next) {\n  await render('index', data); //render index.html from views \n});\n\napp.use(async function ({render}, next) {\n  await render('@alias/index', data);//render index.html from alias path ( \"viewXXX\" )\n});\n```\n### Multiple view folders\nIf folders \"views\" and \"views1\" contain the same file, will be conflict and render any from the same file. \nFor example:\n```javascript\nconst views = require('koa-render-view');\nconst path = require('path');\n\n/*\n  views\n    -index.html\n    \n  views1\n    -index.html\n*/\n\napp.use(views([\n  path.join(__dirname, '/views'),\n  path.join(__dirname, '/views1')\n  ]));\napp.use(async function ({render}, next) {\n  await render('index', data);\n});\n```\nIn this case midleware renders first index.html in its file list. To decide this you need to write part of path\n```javascript\n  await render('views/index', data);// render index.html from views\n  await render('views1/index', data);// render index.html from views1\n```\n\n## API \n\n#### `views( {(string|string[])}, parameters )`\n**By default use [consolidate](https://github.com/tj/consolidate.js)**\n#### [parameters.engine=[consolidate](https://github.com/tj/consolidate.js)]\nTemplate engine. \nCustom engine example: \n```js\n  const engine = {\n    html: (filePath, data) =\u003e {\n      return \"...\";\n   }\n  app.use(views(path.join(__dirname, '/views'), {engine}))\n```\n\n#### [parameters.cache=true] \nIf cache true builds list of files when you create view middleware\n```js\n  fs.writeFileSync(\"views/x.html\", MESSAGE);\n  app.use(views(path.join(__dirname, '/views')))\n  app.use(async function ({render}, next) {\n    await render('x.html', data); //thats work\n  });\n```\n**But in this case, it doesn\\`t work, you need to set cache: false**\n```js\n  app.use(views(path.join(__dirname, '/views')))\n  app.use(async function ({render}, next) {\n    fs.writeFileSync(\"views/x.html\", MESSAGE);\n    await render('x.html', data); //error\n  });\n```\n#### [parameters.extension=\"html\"]\nDefault file extension, if extension === \"html\",then render(\"index\") equals render(\"index.html\")\n\n#### [parameters.recursive=false]\nRecursive read directory\n\n#### [parameters.map={}]\nIt is map extenstion to render engine\n```js\n  app.use(views(path.join(__dirname, '/views'), {\n    xejs: \"ejs\" // render file with extension .xejs by ejs engine\n  }))\n```\n#### [parameters.aliases=[]]\nYou can create alias for path to your views\n- alias - must be a **string starts from @,#,!,$,_ symbols and doesn\\`t contain symbols /\\\\.**\n- path - **absolute** path to directory\n```js\napp.use(views(path.join(__dirname, '/views'), {\n  aliases: [{\n      alias: \"@alias\",\n      path: path.join(__dirname, \"viewXXX\")\n  }]\n}));\n\nawait render('index', data); //render views/index.html\nawait render('@alias/index', data); //render viewXXX/index.html\nawait render('@alias/deep/index', data); //render viewXXX/deep/index.html\n```\n\n#### [parameters.options={}] \nRender options field, these options will pass to render engine as parameters\n\n\n## License\n\nMIT License\n\nCopyright (c) 2017 Artem Shtepenko\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farshtepe%2Fkoa-render-view","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farshtepe%2Fkoa-render-view","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farshtepe%2Fkoa-render-view/lists"}