Let's say I saved a snipplet of a footer. How do I "include" that in my current template?
-
1You can try <%-include('your-path')%> – Hieu.Gi Jan 11 '21 at 06:58
9 Answers
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>

- 1,119
- 6
- 8
-
16Partials are removed from EJS, however you can now use an 'include' syntax. See here: http://stackoverflow.com/a/11835644/175082 – Alexander Mills Jul 10 '15 at 22:56
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'); %>.
You'll likely want to use the raw output tag (<%-) with your include to avoid double-escaping the HTML output.
<ul>
<% users.forEach(function(user){ %>
<%- include('user/show', {user: user}); %>
<% }) %>
</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.

- 1,113
- 8
- 10
-
1does this work if the footer template is in the parent directory, `<% include ../footer %>`? – Jan 25 '14 at 05:33
-
3Yes, that will work too. You can use paths to any ejs file on the file system as long as your node process has read access to it. – DanArl Feb 05 '14 at 01:07
-
Such a weird syntax... to be able to include `../footer` without using quotes or anything. – Eduard Luca Dec 29 '15 at 11:48
-
1This doesn't work without having the path/filename in quotes and parenthesis. EJS docs show <%- include('header'); -%> – devspeter Mar 10 '20 at 22:14
the right syntax is <%- 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'); %>.
You'll likely want to use the raw output tag (<%-) with your include to avoid double-escaping the HTML output.

- 466
- 3
- 13
-
This is what I was looking for: whether the included partial view could be provided its own model data. Do you have a hyperlink to documentation about that? – Mike Finch Aug 10 '23 at 14:35
-
You can include the ejs template by
<% include includes/header.ejs %>

- 2,591
- 3
- 25
- 29
-
1i don't think that will work you need to use <%- instead <% to get rendered output there is different between <% , <%- and <%= please read the docs – Vikas Kandari Dec 22 '19 at 14:48
-
I agree with @VikasKandari . The [EJS docs](https://ejs.co/#docs) recommends using the raw output tag `<%-` because it inserts the unescaped value. – TheLandolorien Feb 01 '20 at 23:07
In Same DIR
<%- include('header'); -%>
Root DIR
<%- include('../partials/header'); -%>
Works with EJS v3.0.1

- 121
- 1
- 4
-
Its working for me. But why we are using like this ? Is there any reason? If any body knows please share. Thanks... – Fawwad Feb 09 '20 at 14:22
-
Easy to use include in ejs
by using this syntax:
<% include filename %>
Note: file should be inside views.

- 2,523
- 2
- 26
- 43

- 79
- 1
- 4
This syntax <% include filename %> is not working anymore. <%- include('[relative path]'); %> this syntax works

- 33
- 3
Assuming you have your partials inside a partial directory
Instead of <% partials/header %>
Try <%- include('partials/header') %>
It works fine for me
To include the file, You are supposed to give the relative address from the file calling the include function. For example, I am assuming that i need to call a file.ejs in my current file.
<%- include('file.ejs') -%>
if my file.ejs is inside a folder, then:
<%- include('folder/file.ejs') -%>