Skip to content
This repository has been archived by the owner on Oct 7, 2018. It is now read-only.

Latest commit

 

History

History
113 lines (94 loc) · 3.33 KB

views.md

File metadata and controls

113 lines (94 loc) · 3.33 KB

Views

In Strapi, views are markup templates that are compiled on the server into HTML pages. In most cases, views are used as the response to an incoming HTTP request.

By default, Strapi doesn't use views. The philosophy of the framework is to separate the reusable backend application logic from the frontend.

If you want to activate views, set the views in ./config/general.json.

For example, if you want to use lodash for .html files and use it by default, you may set up your views object as below:

{
  "views": {
    "map": {
      "html": "lodash"
    },
    "default": "html"
  }
}

Views are defined in your application's ./views directory.

Render a view

Simply use this.render instead of this.body to render a view.

You don't need to specify the view extension if you use the default one sets in config.

Using the config we wrote above with lodash for .html files and use the html extension by default, this example will render ./views/user.html with Lodash as template engine.

yield this.render('user', {
  firstname: 'John',
  lastname: 'Doe'
});
<html>
  <head>...</head>
  <body>
    <p>Firstname: <% firstname %><br>Lastname: <% lastname %></p>
  </body>
</html>

Here is the same example with the jade extension, not used by default:

yield this.render('user.jade', {
  firstname: 'John',
  lastname: 'Doe'
});

Supported template engines

To use a view engine, you should use npm to install it in your project and set the map object in strapi.config.views. For example, if you want to use swig for .html files and hogan for .md files, you may configure the map object as below:

{
  "views": {
    "map": {
      "html": "swig",
      "md": "hogan"
    }
  }
}

Strapi supports all of those view engines: