Javascript 在 EJS 模板引擎中,如何“包含”页脚?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5813771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In EJS template engine, how do I "include" a footer?
提问by TIMEX
Let's say I saved a snipplet of a footer. How do I "include" that in my current template?
假设我保存了一个页脚的片段。我如何在我当前的模板中“包含”它?
回答by Matt H
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:
我知道这个问题已经被标记为已回答,但我相信您正在寻找的是“部分”语法。在 EJS 中,您可以将一个视图模板中的内容包含在另一个视图模板中,如下所示:
<html>
<head></head>
<body>
Blah blah blah
<%- partial('footer') %>
</body>
</html>
回答by DanArl
EJS makes it very simple to use includes. Here is how it is described in the EJS README:
EJS 使得使用包含变得非常简单。以下是 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.
包含与包含语句的模板相关,例如,如果您有“./views/users.ejs”和“./views/user/show.ejs”,您将使用 <% include user/show %>。被包含的文件直接包含在模板中,编译后不执行 IO,因此这些包含的模板可以使用局部变量。
<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.
因此,在您的情况下,如果页脚与要包含它的文件位于同一目录中,则只需将其添加<% include footer %>
到文件中即可。
回答by Hymanal
You can include the ejs template by
您可以通过以下方式包含 ejs 模板
<% include includes/header.ejs %>
回答by tom
In Same DIR<%- include('header'); -%>
在同一目录中<%- include('header'); -%>
Root DIR<%- include('../partials/header'); -%>
根目录<%- include('../partials/header'); -%>
Works with EJS v3.0.1
适用于 EJS v3.0.1
回答by rubendmatos1985
the right syntax is <%- include('<path>', <object with extra parameters>) %>
正确的语法是 <%- include('<path>', <object with extra parameters>) %>
include is a function Includes are relative to the template with the include call. (This requires the 'filename' option.) For example if you have "./views/users.ejs" and "./views/user/show.ejs" you would use <%- include('user/show'); %>.
include 是一个函数 Includes 是相对于带有 include 调用的模板的。(这需要 'filename' 选项。)例如,如果您有“./views/users.ejs”和“./views/user/show.ejs”,您将使用 <%- include('user/show') ; %>。
You'll likely want to use the raw output tag (<%-) with your include to avoid double-escaping the HTML output.
您可能希望在包含中使用原始输出标记 (<%-) 以避免双重转义 HTML 输出。
回答by Bijayee Saswata
Easy to use include in ejs
by using this syntax:
ejs
使用以下语法易于使用 include in :
<% include filename %>
<% include filename %>
Note: file should be inside views.
注意:文件应该在视图中。