Let's say I saved a snipplet of a footer. How do I "include" that in my current template?
-
1You can try – Hieu - 7347514 Jan 11 '21 at 06:58
8 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,121
- 6
- 8
-
14Partials 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 %>. 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.
- 1,083
- 8
- 10
-
1
-
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 – devspeter Mar 10 '20 at 22:14
You can include the ejs template by
<% include includes/header.ejs %>
- 2,461
- 2
- 23
- 28
-
1
-
I agree with @VikasKandari . The [EJS docs](https://ejs.co/#docs) recommends using the raw output tag ` – TheLandolorien Feb 01 '20 at 23:07
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.
- 420
- 3
- 11
In Same DIR
<%- include('header'); -%>
Root DIR
<%- include('../partials/header'); -%>
Works with EJS v3.0.1
- 111
- 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,373
- 1
- 24
- 41
- 79
- 1
- 4
This syntax <% include filename %> is not working anymore. <%- include('[relative path]'); %> this syntax works
- 23
- 3
Assuming you have your partials inside a partial directory
Instead of <% partials/header %>
Try <%- include('partials/header') %>
It works fine for me