簡體   English   中英

在 ejs 文件中包含 header 和頁腳不起作用

[英]include of header and footer in ejs file not working

伙計們,我正在雲 9 中開發節點/快速應用程序。 https://webdevcamp-miatech.c9users.io/但是,我的 home.ejs 文件中的包含文件不起作用。 我有一個 header.ejs 和 footer.ejs,我將它們包含在我的 home.ejs 文件中。 但是當我 go 調用默認路由“/”回家時。 它給了我。 下面是c9.io https://ide.c9.io/miatech/webdevcamp上的app 這個錯誤信息。

Error: Could not find include include file.
    at getIncludePath (/home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:152:13)
    at includeSource (/home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:276:17)
    at /home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:629:26
    at Array.forEach (native)
    at Object.generateSource (/home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:605:15)
    at Object.compile (/home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:509:12)
    at Object.compile (/home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:358:16)
    at handleCache (/home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:201:18)
    at tryHandleCache (/home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:223:14)
    at View.exports.renderFile [as engine] (/home/ubuntu/workspace/EJSDemo/node_modules/ejs/lib/ejs.js:437:10)

如果我從 home.ejs 中刪除頁腳和 header 的包含,它就會得到修復。 所以它必須是我包含它的方式或路徑。 如果有人可以提供幫助。 感謝 home.ejs

<% include ./partials/header %>
<h1>Hello World</h1>
<img src="http://lorempixel.com/400/200/">
<% include ./partials/footer %>

header.ejs

<!DOCTYPE html>
<html>
<head>
    <title>EJSDemo</title>
    <link rel="stylesheet" type="text/css" href="/app.css">
</head>
<body>

頁腳.ejs

        <p>trademark 2018</p>
    </body>
</html>

最后我的 app.js

var express = require("express");
var app = express();

//tell express to serve the content of public dir
app.use(express.static("public"));
app.set("view engine", "ejs");

//default app route
app.get("/", function(req, res) {
    res.render("home.ejs");
});

// /fallinlove route
app.get("/fallinlove/:thing", function(req, res) {
    var thing = req.params.thing;
    //rendering love.ejs and passing variable to template
    res.render("love.ejs", {thingVar:thing});
});

// /post route
app.get("/posts", function(req, res) {
    var posts = [
            {title: "Post 1 ", author: "Charlie"},
            {title: "My adorable pet bunny", author: "Susy"},
            {title: "Can you believe this pomsky?", author: "Colt"}
        ]
    res.render("posts.ejs", {posts: posts});
});


//start server
app.listen(process.env.PORT, process.env.IP, function() {
    console.log("server started!..");
});

我遇到了同樣的問題,這個新的(er)EJS 語法工作得很好。

// <%- include("partials/header") %>
// <%- include("partials/footer") %>

對於任何面臨這個問題的人來說,新的語法是

<%- include('partials/header') %>

提示:您應該包括:

app.set("view engine", "ejs");

我遇到了同樣的問題,但以下代碼對我有用

<%- include('header'); -%>

<!-- your HTML code --> 

<%- include('footer'); -%>

我不能說沒有看到您的文件夾結構,但最可能的問題可能是您在<% include ./partials/header.ejs %>給出的路徑。 通過提供此路徑,您告訴ejs partials文件夾和home.ejs文件在同一目錄中。

編輯您的問題以向我們展示您的文件夾的組織方式。 一旦你這樣做,我們就可以排除這個問題。

編輯:其他模板中的包含是否有效? 或者它只是home.ejs

我的代碼現在可以正常工作並顯示如下:

 <%- include('header'); -%>
<%- include('footer'); -%>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM