Layouts in Express 3 and EJS

前端 未结 3 851
野的像风
野的像风 2021-02-01 22:49

In version 3 of Express some features were removed:

the concept of a \"layout\" (template engine specific now)
partial() (template engine specific)
3条回答
  •  清歌不尽
    2021-02-01 23:17

    It seems that from Express 3, layout feature is delegated to the responsibility of template engines. You can use ejs-locals (https://github.com/RandomEtc/ejs-locals) for layout.

    Install ejs-locals

    npm install ejs-locals --save
    

    Use ejs-locals as your app engine in app.js

    var express = require('express');
    var engine = require('ejs-locals');
    ...
    
    app.engine('ejs', engine);
    app.set('view engine', 'ejs');
    

    Now you can use layout

    layout.ejs
    
      <%- body %>
    
    
    index.ejs
    <% layout('layout') -%>
    
    
    ...

    Another option is to use express-partials (https://github.com/publicclass/express-partials). The two do the same thing, so it's just your choice.

提交回复
热议问题