[英]EJS Relative Path “include” Issue in HTML File
I have a problem with my "index.ejs" file...我的“index.ejs”文件有问题...
The current content of the ejs file: ejs文件的当前内容:
<!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:文件的当前位置是这样的:
I am really stuck at giving relative path as it throws error as soon as I use this in ejs file:我真的坚持给出相对路径,因为一旦我在 ejs 文件中使用它就会抛出错误:
<%- include(__dirname + '/views/partials/so_footer.ejs') %>
<%- include(__dirname + '/views/partials/so_footer.ejs') %>
Reference error:参考错误:
dirname is not defined.目录名未定义。
Thanks for the responses.感谢您的回复。
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:例如,从
pages/index.ejs
到包含partials/so_head-tag.ejs
,您可以使用:
<%- include('../partials/so_head-tag.ejs') %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.