在 ejs 文件中包含页眉和页脚不起作用

问题描述 投票:0回答:5

伙计们,我正在云 9 中开发节点/express 应用程序。https://webdevcamp-miatech.c9users.io/但是,我的 home.ejs 文件中的包含文件不起作用。我有一个 header.ejs 和 footer.ejs,我将其包含到我的 home.ejs 文件中。但是当我去调用默认路由“/”回家时。它给了我。这是 c9.io 上的应用程序 https://ide.c9.io/miatech/webdevcamp 这个错误信息。

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 中删除页脚和页眉的包含内容,它就会得到修复。所以它必须是我包含它的方式或路径。如果有人可以帮忙的话。谢谢 首页.ejs

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

标题.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!..");
});
node.js express include ejs
5个回答
13
投票

我遇到了同样的问题,这个新的 EJS 语法工作得很好。

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

9
投票

对于任何面临该问题的人来说,新语法是

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

提示:您应该包括:

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

3
投票

我面临同样的问题,但以下代码对我有用

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

<!-- your HTML code --> 

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

0
投票

我不能说没有看到你的文件夹结构,但最有可能的问题可能是你在

<% include ./partials/header.ejs %>
中给出的路径。通过提供此路径,您告诉 ejs partials 文件夹和 home.ejs 文件位于同一目录中。

编辑您的问题,向我们展示您的文件夹的组织方式。一旦您这样做,我们就可以排除该问题。

编辑:其他模板中的包含内容是否有效?或者只是 home.ejs


0
投票

我的代码现在可以正常工作并显示如下:

 <%- include('header'); -%>
<%- include('footer'); -%>
© www.soinside.com 2019 - 2024. All rights reserved.