Using interpolation within Node.js EJS includes

后端 未结 4 1022
天涯浪人
天涯浪人 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:28

    In the version 2 of EJS, the include function does it well. With it, includes are inserted at runtime so variables can be used as pathnames.

    In this case, the solution may be :

    <%- include('contents/' + contents) %>
    

    The function can also have another argument if necessary :

    <%- include('mypathname', {foo:"bar"}) %>
    

    The pathname has to be relative to the template which calls the function.

提交回复
热议问题