How to enable partial parts of EJS using an absolute path

I cannot include partial parts of EJS using the absolute path, for example /partials/footer.ejs When I say that EJS is trying to look at this file in my file system root.

So, currently I am doing this <% include ../partials/footer.ejs %> But it gets too heavy if I work on an ejs file like / services / webapps / app 1.ejs then I should mention partial, like <% include ../../../partials/footer.ejs %> , and if I reorganize this file, it breaks easily.

So how to enable scores with an absolute path? so i can just say /partials/footer.ejs

+7
express ejs
source share
1 answer

It seems that EJS is not yet supported. However, I found a workaround for this problem. In the above setup, the pain point is for all partial files that you need to mention relative paths, and if you use refactoring code that gets painful. Thus, instead of mentioning the relative path in each of them, I declare the rootPath variable once at the top of the page, and there I give the path to get to the house. So in each inclusion, I can simply specify the relative path as the path from root.

For example, in guide/index.ejs I mention the following at the top of the ejs file

<% var rootPath = '../'; %>

and the code in the ejs file looks below

<%- include(rootPath + 'partials/header'); %>

Your html code

<%- include(rootPath + 'partials/footer'); %>

So in case I refactor index.ejs to some other folder I need to do is change the rootPath value only right away.

+7
source share

All Articles