'In EJS template engine, how do I "include" a footer?

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



Solution 1:[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>

Solution 2:[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.

Solution 3:[3]

You can include the ejs template by

<% include includes/header.ejs %>

Solution 4:[4]

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.

Solution 5:[5]

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

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

Works with EJS v3.0.1

Solution 6:[6]

Easy to use include in ejs by using this syntax:

<% include filename %>

Note: file should be inside views.

Solution 7:[7]

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

Solution 8:[8]

Assuming you have your partials inside a partial directory

Instead of <% partials/header %>

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

It works fine for me

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt H
Solution 2 DanArl
Solution 3 Jackal
Solution 4 rubendmatos1985
Solution 5 tom
Solution 6 DarkSuniuM
Solution 7 kulinos
Solution 8 VatsalVatsyayan