【问题标题】:"SyntaxError: Unexpected token '/' ... while compiling ejs" happening in include statement“SyntaxError: Unexpected token '/' ... while compile ejs”发生在 include 语句中
【发布时间】:2020-03-21 12:43:21
【问题描述】:

我似乎无法找到发生这种情况的原因,我的大多数应用程序都有几乎相同的代码,它几乎是样板,我只是试图将 HTML 的顶部分成 header.ejs 和底部放入footer.ejs 文件,然后将它们包含到我的index.ejs 中。当我将所有代码保留在 index.ejs 文件中时,它就可以了。我还尝试删除像 这样的空格,添加'-' ,即使我有相同的代码 - 在我的文件中使用相同的语法。

Index.ejs

<% include ./partials/header.ejs %>

<h1>YO HO HO LET THIS WORK!</h1>

<% include ./partials/footer.ejs %>

Header.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>P2</title>
    <link rel="stylesheet" href="/stylesheets/app.css">
</head>
<body>

页脚.ejs

</body>
</html>

【问题讨论】:

    标签: html node.js ejs


    【解决方案1】:

    尝试将其用于 index.ejs

    <%=include partials/header.ejs%>
    
    <h1>YO HO HO LET THIS WORK!</h1>
    
    <%=include partials/footer.ejs%>
    

    【讨论】:

    • 您是否尝试从每行的开头删除 ./ 以便它只是 partials/header.ejs @Jack
    • 是的,没有变化。
    【解决方案2】:

    您使用的是哪个版本的 ejs?

    我注意到最近的版本从 2.7.4 升级。到 3.0.1 意味着包括需要看起来更像:

    <%- include('partials/header.ejs') %>
    
    <h1>YO HO HO LET THIS WORK!</h1>
    
    <%- include('partials/footer.ejs') %>
    

    【讨论】:

      【解决方案3】:

      您好,我在这里找到了一个适合我的解决方案是使用的语法。

       "dependencies": {
          "ejs": "^3.0.1",
       }"
      
      <%- include('../pages/header.ejs') %>
      
          <h1>Hi ejs </h1>
      <%- include('../pages/footer.ejs') %>
      

      文件夹: -页面: -header.ejs -footer.ejs

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多