首页 > 解决方案 > 如何在 Strapi 中使用 EJS

问题描述

我尝试了官方文档,但这不起作用。

https://github.com/strapi/strapi-docs/blob/master/files/views.md

标签: templatesejsstrapi

解决方案


这是有关如何将 EJS 与 Strapi 一起使用的指南

  • 安装了strapi-hook-ejs
  • 更新 .config/hook.json
...
{
  "ejs": {
    "enabled": true,
    "layout": false,
    "viewExt": "ejs",
    "partial": true,
    "cache": false,
    "debug": true
  }
}
...
  • 将此添加到控制器中,例如./api/api-name/controllers/api-name.js
module.exports = {
  //GET /index
  index: async (ctx) => {
    ctx.render('home', {title: 'Hello world'});
  }
};
  • 在根文件夹中创建目录视图
  • 在 ./views 中添加您的 ejs 模板
  • 示例 ./views/home.ejs

<%= title %>

旁注:在上面的示例中,我在控制器 api-name.js 文件中使用了“索引”。确保您的 API ./api/api-name/config/routes.json指向它。


推荐阅读