In EJS template engine, how do I “include” a footer?

此生再无相见时 提交于 2019-11-29 10:34:22

问题


Let's say I saved a snipplet of a footer. How do I "include" that in my current template?


回答1:


I know this question has already been marked as answered, but I believe what you were looking for is the 'partial' syntax. In EJS, you can include the content from one view template in another like so:

<html>
  <head></head>
  <body>
    Blah blah blah
    <%- partial('footer') %>    
  </body>
</html>



回答2:


EJS makes it very simple to use includes. Here is how it is described in the EJS README:

Includes are relative to the template with the include statement, for example if you have "./views/users.ejs" and "./views/user/show.ejs" you would use <% include user/show %>. The included file(s) are literally included into the template, no IO is performed after compilation, thus local variables are available to these included templates.

<ul>
  <% users.forEach(function(user){ %>
    <% include user/show %>
  <% }) %>
</ul>

So, in your case, if the footer resides in the same directory as the file you want to include it in, you would simply add <% include footer %> to your file.




回答3:


You can include the ejs template by

<% include includes/header.ejs %>



回答4:


Easy to use include in ejs by using this syntax:

<% include filename %>

Note: file should be inside views.



来源:https://stackoverflow.com/questions/5813771/in-ejs-template-engine-how-do-i-include-a-footer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!