{"name":"@eggjs/view","version":"4.0.0-beta.20","keywords":["egg","eggPlugin","egg-plugin","egg-view","view"],"author":{"name":"popomore","email":"sakura9515@gmail.com"},"license":"MIT","_id":"@eggjs/view@4.0.0-beta.20","maintainers":[{"name":"gxkl","email":"gxkl203@gmail.com"},{"name":"eggjs-admin","email":"fengmk2+eggjs@gmail.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"},{"name":"atian25","email":"atian25@qq.com"},{"name":"hyj1991","email":"yeekwanvong@gmail.com"},{"name":"killagu","email":"killa07071201@gmail.com"},{"name":"coolme200","email":"2001-wms@163.com"},{"name":"akitasummer","email":"akitasummer@outlook.com"}],"homepage":"https://github.com/eggjs/egg/tree/next/plugins/view","bugs":{"url":"https://github.com/eggjs/egg/issues"},"dist":{"shasum":"54e2357ba97e958e48c517ade7a52ab2f3361826","tarball":"http://registry-npm.ywwl.com/@eggjs/view/download/@eggjs/view-4.0.0-beta.20.tgz","fileCount":35,"integrity":"sha512-MMn2Rp2Ke7BR2pmAUO0tO09VFPW7Q6Fyvj6/NNkMUIwh0iT498lQaKcwQGUwkXyrAyyPdkfn1RiZdu7NilQ+mA==","signatures":[{"sig":"MEUCID6WwNhRuK27WfcJpfnXEE/Xm6zGNhrHTsTh5Hqb4S6mAiEAlBL6EuZUC/jzVozH2ROwamIrGnxKUyvUdbxweLupwgo=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@eggjs%2fview@4.0.0-beta.20","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":30130,"size":8660,"noattachment":false,"key":"/@eggjs/view/-/@eggjs/view-4.0.0-beta.20.tgz"},"main":"./dist/index.js","type":"module","_from":"file:eggjs-view-4.0.0-beta.20.tgz","types":"./dist/index.d.ts","module":"./dist/index.js","engines":{"node":">=22.18.0"},"exports":{".":"./dist/index.js","./lib":"./dist/lib/index.js","./types":"./dist/types.js","./package.json":"./package.json","./lib/context_view":"./dist/lib/context_view.js","./lib/view_manager":"./dist/lib/view_manager.js","./app/extend/context":"./dist/app/extend/context.js","./config/config.local":"./dist/config/config.local.js","./config/config.default":"./dist/config/config.default.js","./app/extend/application":"./dist/app/extend/application.js"},"scripts":{"lint":"oxlint --type-aware","test":"vitest run","build":"tsdown","typecheck":"tsc --noEmit"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:87bf230b-4257-4740-8755-64d805aedd22"}},"_resolved":"/tmp/7ab859dafe735497bac7301284062515/eggjs-view-4.0.0-beta.20.tgz","eggPlugin":{"name":"view"},"_integrity":"sha512-MMn2Rp2Ke7BR2pmAUO0tO09VFPW7Q6Fyvj6/NNkMUIwh0iT498lQaKcwQGUwkXyrAyyPdkfn1RiZdu7NilQ+mA==","repository":{"url":"git://github.com/eggjs/egg.git","type":"git","directory":"plugins/view"},"_npmVersion":"11.6.1","description":"Base view plugin for egg","directories":{},"_nodeVersion":"22.20.0","dependencies":{"utility":"^2.5.0","is-type-of":"^2.2.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"tsdown":"^0.15.4","vitest":"4.0.0-beta.16","typescript":"^5.9.3","@eggjs/mock":"7.0.0-beta.20","@eggjs/tsconfig":"3.1.0-beta.20"},"peerDependencies":{"egg":"4.1.0-beta.20"},"_npmOperationalInternal":{"tmp":"tmp/view_4.0.0-beta.20_1759594116383_0.23049920575879868","host":"s3://npm-registry-packages-npm-production"},"readme":"# @eggjs/view\n\n[![NPM version](https://img.shields.io/npm/v/@eggjs/view.svg?style=flat-square)](https://npmjs.org/package/@eggjs/view)\n[![NPM download](https://img.shields.io/npm/dm/@eggjs/view.svg?style=flat-square)](https://npmjs.org/package/@eggjs/view)\n[![Node.js Version](https://img.shields.io/node/v/@eggjs/view.svg?style=flat)](https://nodejs.org/en/download/)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)\n![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/view)\n\nBase view plugin for egg\n\n**it's a plugin that has been built-in for egg.**\n\n## Install\n\n```bash\nnpm i @eggjs/view\n```\n\n## Usage\n\n```js\n// {app_root}/config/plugin.ts\nexport default {\n  view: {\n    enable: true,\n    package: '@eggjs/view',\n  },\n};\n```\n\n## Use a template engine\n\n[@eggjs/view] don't have build-in view engine, So you should choose a template engine like [ejs], and install [egg-view-ejs] plugin.\n\nYou can choose a template engine first, link [ejs], so we use [egg-view-ejs] plugin.\n\n`egg-view` is in [eggjs], so you just need configure [egg-view-ejs].\n\n```js\n// config/plugin.ts\nexport default {\n  ejs: {\n    enable: true,\n    package: 'egg-view-ejs',\n  },\n};\n```\n\nConfigure the mapping, the file with `.ejs` extension will be rendered by ejs.\n\n```js\n// config/config.default.ts\nexport default {\n  view: {\n    mapping: {\n      '.ejs': 'ejs',\n    },\n  },\n};\n```\n\nIn controller, you can call `ctx.render`.\n\n```js\nexport default (app: Application) => {\n  return class UserController extends app.Controller {\n    async list() {\n      const { ctx } = this;\n      await ctx.render('user.ejs');\n    }\n  };\n}\n```\n\nIf you call `ctx.renderString`, you should specify viewEngine in viewOptions.\n\n```js\nexport default (app: Application) => {\n  return class UserController extends app.Controller {\n    async list() {\n      const { ctx } = this;\n      ctx.body = await ctx.renderString(\n        '<%= user %>',\n        { user: 'popomore' },\n        {\n          viewEngine: 'ejs',\n        }\n      );\n    }\n  };\n}\n```\n\n## Use multiple view engine\n\n[egg-view] support multiple view engine, so you can use more than one template engine in one application.\n\nIf you want add another template engine like [nunjucks], then you can add [egg-view-nunjucks] plugin.\n\nConfigure the plugin and mapping\n\n```js\n// config/plugin.ts\nexport default {\n  ejs: {\n    enable: true,\n    package: 'egg-view-ejs',\n  },\n  nunjucks: {\n    enable: true,\n    package: 'egg-view-nunjucks',\n  },\n};\n```\n\n```js\n// config/config.default.ts\nexport default {\n  view: {\n    mapping: {\n      '.ejs': 'ejs',\n      '.nj': 'nunjucks',\n    },\n  },\n};\n```\n\nYou can simply render the file with `.nj` extension.\n\n```js\nawait ctx.render('user.nj');\n```\n\n## How to write a view plugin\n\nYou can use [@eggjs/view]'s API to register a plugin.\n\n### View engine\n\nCreate a view engine class first, and implement `render` and `renderString`, if the template engine don't support, just throw an error.\nThe view engine is context level, so it receive ctx in `constructor`.\n\n```js\n// lib/view.ts\nexport default class MyView {\n  constructor(ctx) {\n    // do some initialize\n    // get the plugin config from `ctx.app.config`\n  }\n\n  async render(fullpath: string, locals: Record<string, any>) {\n    return myengine.render(fullpath, locals);\n  }\n\n  async renderString() {\n    throw new Error('not implement');\n  }\n}\n```\n\n`render` and `renderString` support generator function, async function, or normal function return a promise.\n\nIf the template engine only support callback, you can wrap it by Promise.\n\n```js\nclass MyView {\n  render(fullpath, locals) {\n    return new Promise((resolve, reject) => {\n      myengine.render(fullpath, locals, (err, result) => {\n        if (err) {\n          reject(err);\n        } else {\n          resolve(result);\n        }\n      });\n    });\n  }\n}\n```\n\nThese methods receive three arguments, `renderString` will pass tpl as the first argument instead of name in `render`.\n\n`render(name, locals, viewOptions)`\n\n- name: the file path that can resolve from root (`app/view` by default)\n- locals: data used by template\n- viewOptions: the view options for each render, it can override the view default config in `config/config.default.js`. Plugin should implement it if it has config.\n  When you implement view engine, you will receive this options from `render`, the options contain:\n  - root: egg-view will resolve the name to full path, but seperating root and name in viewOptions.\n  - name: the original name when call render\n  - locals: the original locals when call render\n\n`renderString(tpl, locals, viewOptions)`\n\n- tpl: the template string instead of the file, using in `renderString`\n- locals: same as `render`\n- viewOptions: same as `render`\n\n### Register\n\nAfter define a view engine, you can register it.\n\n```js\n// app.ts\nimport type { Application } from 'egg';\nimport MyView from './lib/view';\n\nexport default (app: Application) => {\n  app.view.use('myName', MyView);\n}\n```\n\nYou can define a view engine name, normally it's a template name.\n\n### Configure\n\nDefine plugin name and depend on [@eggjs/view]\n\n```json\n{\n  \"eggPlugin\": {\n    \"name\": \"myName\",\n    \"dependencies\": [\"view\"]\n  }\n}\n```\n\nSet default config in `config/config.default.ts`, the name is equals to plugin name.\n\n```js\nexport default {\n  myName: {},\n};\n```\n\nSee some examples\n\n- [egg-view-ejs]\n- [egg-view-nunjucks]\n\n## Configuration\n\n### Root\n\nRoot is `${baseDir}/app/view` by default, but you can define multiple directory, seperated by `,`.\n[@eggjs/view] will find a file from all root directories.\n\n```js\nexport default (appInfo: EggAppInfo) => {\n  const baseDir = appInfo.baseDir;\n  return {\n    view: {\n      root: `${baseDir}/app/view,${baseDir}/app/view2`,\n    },\n  };\n}\n```\n\n### defaultExtension\n\nWhen render a file, you should specify a extension that let [@eggjs/view] know whitch engine you want to use.\nHowever you can define `defaultExtension` without write the extension.\n\n```js\n// config/config.default.ts\nexport default {\n  view: {\n    defaultExtension: '.html',\n  },\n};\n\n// controller\nexport default (app: Application) => {\n  return class UserController extends app.Controller {\n    async list() {\n      const { ctx } = this;\n      // render user.html\n      await ctx.render('user');\n    }\n  }\n}\n```\n\n### viewEngine and defaultViewEngine\n\nIf you are using `renderString`, you should specify viewEngine in view config, see example above.\n\nHowever, you can define `defaultViewEngine` without set each time.\n\n```js\n// config/config.default.ts\nexport default {\n  view: {\n    defaultViewEngine: 'ejs',\n  },\n};\n```\n\nsee [config/config.default.ts](https://github.com/eggjs/egg/blob/next/plugins/view/src/config/config.default.ts) for more detail.\n\n## Questions & Suggestions\n\nPlease open an issue [here](https://github.com/eggjs/egg/issues).\n\n## License\n\n[MIT](LICENSE)\n\n## Contributors\n\n[![Contributors](https://contrib.rocks/image?repo=eggjs/egg)](https://github.com/eggjs/egg/graphs/contributors)\n\nMade with [contributors-img](https://contrib.rocks).\n\n[eggjs]: https://eggjs.org\n[ejs]: https://github.com/mde/ejs\n[egg-view-ejs]: https://github.com/eggjs/egg-view-ejs\n[@eggjs/view]: https://github.com/eggjs/egg/tree/next/plugins/view\n[nunjucks]: http://mozilla.github.io/nunjucks\n[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks\n","publish_time":1759594116636,"_cnpm_publish_time":1759594116636,"dist-tags":{"beta":"4.0.2-beta.27","latest":"4.1.0","rc":"4.0.2-rc.1"}}