Search code examples
templatesejsstrapi

How to use EJS in Strapi


I tried the official documentation, but this does not work.

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


Solution

  • Here is a guide on How to use EJS with Strapi

    • Installed strapi-hook-ejs
    • Update the .config/hook.json with
    ...
    {
      "ejs": {
        "enabled": true,
        "layout": false,
        "viewExt": "ejs",
        "partial": true,
        "cache": false,
        "debug": true
      }
    }
    ...
    
    • Add this to the controller, say ./api/api-name/controllers/api-name.js
    module.exports = {
      //GET /index
      index: async (ctx) => {
        ctx.render('home', {title: 'Hello world'});
      }
    };
    
    • Create a directory views in root folder
    • Add your ejs templates in the ./views
    • Example ./views/home.ejs

    <%= title %>

    Side note: In the above example, I have used the "index" in the controller api-name.js file. Make sure your API ./api/api-name/config/routes.json point to it.