Using interpolation within Node.js EJS includes

后端 未结 4 1068
天涯浪人
天涯浪人 2021-02-07 09:46

My Express app is using EJS, and my views directory looks like this:

./views
  ./contents
    home.ejs
  ./includes
    header.ejs
    footer.ejs
  layout.ejs
         


        
4条回答
  •  耶瑟儿~
    2021-02-07 10:39

    I think there is no way to do this kind of dynamic includes in EJS. It might break the separation of business logic and view. The solution can be to rendering the subtemplate in the controller, and passing its content to the layout.

    For rendering subtemplate in the controller use something like this:

    var ejs = require('ejs'),
    , fs = require('fs')
    , home = ejs.render(fs.readFileSync("contents/home.ejs", "utf-8"))
    

提交回复
热议问题