61

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

TIMEX
  • 238,746
  • 336
  • 750
  • 1,061

8 Answers8

95

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>
Matt H
  • 1,121
  • 6
  • 8
  • 14
    Partials 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
52

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.

DanArl
  • 1,083
  • 8
  • 10
  • 1
    does this work if the footer template is in the parent directory, ``? –  Jan 25 '14 at 05:33
  • 3
    Yes, 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
  • 1
    This doesn't work without having the path/filename in quotes and parenthesis. EJS docs show – devspeter Mar 10 '20 at 22:14
19

You can include the ejs template by

<% include includes/header.ejs %>
Jackal
  • 2,461
  • 2
  • 23
  • 28
17

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.

rubendmatos1985
  • 420
  • 3
  • 11
10

In Same DIR
<%- include('header'); -%>

Root DIR
<%- include('../partials/header'); -%>

Works with EJS v3.0.1

tom
  • 111
  • 1
  • 4
4

Easy to use include in ejs by using this syntax:

<% include filename %>

Note: file should be inside views.

DarkSuniuM
  • 2,373
  • 1
  • 24
  • 41
1

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

kulinos
  • 23
  • 3
0

Assuming you have your partials inside a partial directory

Instead of <% partials/header %>

Try <%- include('partials/header') %>

It works fine for me