Codecygen
Codecygen

Reputation: 121

EJS Relative Path "include" Issue in HTML File

I have a problem with my "index.ejs" file...

The current content of the ejs file:

<!DOCTYPE html>
<html lang="en" dir="ltr">

  <!-- THIS SECTION IS FOR <head> TAG THAT WILL BE STORED INSIDE "so_head-tag.ejs" -->
  <%- include('/home/username/Desktop/www/website.com/views/partials/so_head-tag.ejs') %>

<body>

  <!-- THIS SECTION IS FOR NAVIGATION BAR THAT WILL BE STORED INSIDE "so_header.ejs" -->
  <%- include('/home/username/Desktop/www/website.com/views/partials/so_header.ejs') %>

  <div class="filler">

  </div>

  <!-- THIS SECTION IS FOR FOOTER BAR THAT WILL BE STORED INSIDE "so_footer.ejs" -->
  <%- include('/home/username/Desktop/www/website.com/views/partials/so_footer.ejs') %>

  <!-- THIS SECTION IS FOR JAVASCRIPT FILES THAT WILL BE STORED INSIDE "so_javacript.ejs" -->
  <%- include('/home/username/Desktop/www/website.com/views/partials/so_javacript.ejs') %>

</body>

</html>

In the current configuration, the pages are rendered perfectly. The only problem in here is though, we give absolute path. So when I try to copy files to a new server, because the username may be different, I have to manually change those usernames into the every server I copy these files. I would like to use some kind of a relative path method.

Current placements of the files are like this:

enter image description here

I am really stuck at giving relative path as it throws error as soon as I use this in ejs file:

<%- include(__dirname + '/views/partials/so_footer.ejs') %>

Reference error:

dirname is not defined.

Thanks for the responses.

Upvotes: 1

Views: 451

Answers (1)

Đăng Khoa Đinh
Đăng Khoa Đinh

Reputation: 5411

To include a sub-template file, you need to provide the relative path from the parent template to the sub-template.

For example, from pages/index.ejs, to include partials/so_head-tag.ejs, you can use:

 <%- include('../partials/so_head-tag.ejs') %>

Upvotes: 1

Related Questions