【问题标题】:EJS Unexpected token / - Using PartialsEJS 意外令牌 / - 使用 Partials
【发布时间】:2020-04-16 08:34:00
【问题描述】:

我试图在我的网络应用程序中使用部分,但在仪表板页面上,我收到以下错误:

SyntaxError: Unexpected token / in /Users/ng235/Documents/Work/app-1/views/dashboard.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://Work.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
    at new Function (<anonymous>)
    at Template.compile (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:626:12)
    at Object.compile (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:366:16)
    at handleCache (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:215:18)
    at tryHandleCache (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:254:16)
    at View.exports.renderFile [as engine] (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:459:10)
    at View.render (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/application.js:640:10)
    at Function.render (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/response.js:1012:7)
    at /Users/ng235/Documents/Work/app-1/app.js:51:9
    at Layer.handle [as handle_request] (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/layer.js:95:5)
    at /Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/index.js:281:22

我包含部分内容的方式是:

<% include ./partials/messages %>

我的文件系统是 app-2 -> views -> partials -> messages.ejs

那个文件是:

<% if (message && message.length > 0) { %>
<div class="alert alert-primary my-5"><%= message %></div>
<% } %>

我的仪表板页面(我尝试添加部分内容的页面)是(不尝试泄漏任何内容):

<div class="container text-center">
    <h1 class="display-4 my-5">Dashboard</h1>

    <% include ./partials/messages %>
</div>

感谢您的所有帮助。

【问题讨论】:

  • 请在下面查看我的回答,其中详细说明了 EJS 现在如何具有不同的语法。谢谢

标签: node.js ejs partials


【解决方案1】:

3.x.x 版本开始,EJS 现在有不同的部分语法

&lt;%- include ./partials/file %&gt;&lt;% include ./partials/file %&gt; 替换为&lt;%- include('./partials/file') %&gt;

新的语法是: &lt;%- include('./partials/file') %&gt;

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    相关资源
    最近更新 更多