Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js ejs中的布局'yield'方法是什么?_Node.js_Ejs_Express - Fatal编程技术网

Node.js ejs中的布局'yield'方法是什么?

Node.js ejs中的布局'yield'方法是什么?,node.js,ejs,express,Node.js,Ejs,Express,我只是从node.js+express+ejs开始。我在任何地方都找不到如何将请求的ejs文件拉入布局文件 我很清楚,收益率在这里是不对的 e、 g layout.ejs EJS布局 index.ejs 你好 终于找到了一些express应用程序的源代码: <%- body %> 我想我可以帮你解决这个问题。我会给你一些解释 ejs确实是您需要的布局,它由代码片段构建而成:) 我的布局.ejs看起来像: <!DOCTYPE html> <html xmlns=

我只是从node.js+express+ejs开始。我在任何地方都找不到如何将请求的ejs文件拉入布局文件

我很清楚,
收益率
在这里是不对的

e、 g

layout.ejs


EJS布局
index.ejs

你好


终于找到了一些express应用程序的源代码:

<%- body %>

我想我可以帮你解决这个问题。我会给你一些解释

ejs确实是您需要的布局,它由代码片段构建而成:)

我的布局.ejs看起来像:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
    <title><%- title %></title>
    <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
</head>

<body>
    <!-- total container -->
    <header>

        <%- partial('topic.ejs') %>

        <%- body %>
     </header>
</body>
</html>
说明:xyz是一个随机名称。你自己选一个。此名称现在是您的URL。你不明白吗?看看下面的例子。在通过执行app.js启动服务器之后,服务器运行在一个特定的端口上(我想是默认的3000)。通常index.html的URL应该是“localhost:3000/index.html”。在浏览器的地址栏中键入它。你的网站应该显示。现在试试这个:

localhost:PORT/xyz

在前面显示的app.get-methode中,您明确地说您的app.js:“/xyz”-路径后面有“index.ejs”-文件。但这到底意味着什么呢?
这意味着您现在可以在浏览器的地址栏中键入“locallhost:PORT/xyz”,并显示primal index.html网站的内容,但您将看到layout.ejs.Magic!的生成内容

背后的逻辑:(如果您再次查看layout.ejs)
使用该命令,您只需将一段源代码加载到布局中。只需执行以下操作:运行站点后,右键单击它,让您显示源代码。它应该是常见的HTML源代码。实际上,它是layout.ejs的源代码,占用了您的一些代码片段。

一体式:
使用layout.ejs中的命令,您可以加载一段代码。=“index.ejs”、“contact.ejs”等等。对于每个.ejs文件,您都需要将app.js扩展到其“get”方法(示例如下)。如果您有更多站点(当然您没有一个站点),则需要将新站点的代码片段放入.ejs文件中(例如:contact.html=>contact.ejs)。您还需要将app.js文件扩展到以下位置:

app.get('/contact', function(req, res){
        res.render('contact.ejs', { title: 'My Site' });
    });


不要忘记更改.ejs文件中的链接: onclick=“window.location.replace('contact.html')”成为您在app.get-methode中选择的名称。例如,它将更改为onclick=“window.location.replace('contact')”

onclick=“window.location.replace(“contact.html”)变为 onclick=“window.location.replace(“联系方式””)


您只需链接到URL名称,而不是文件。App.js现在将为您处理此问题:)

我不想提一个老话题,但他的回答没有提供任何解释,什么都没有。这个语句属于哪里?它做什么?你如何在上下文中使用它?这是一个令人沮丧的答案,我不知道它为什么有这么多的选票。这个问题假设理解在其他模板语言(如erb)中的含义。这就是为什么是的,但无论如何,“布局”中的屈服点是您希望“模板”呈现的位置,布局可以被视为父模板。它不在ejs文档中,因为它不是ejs功能。布局是由express添加的,因此它们在每个引擎中的工作方式相同。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
    <title><%- title %></title>
    <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
</head>

<body>
    <!-- total container -->
    <header>

        <%- partial('topic.ejs') %>

        <%- body %>
     </header>
</body>
</html>
<!-- frame of topic -->
<div id="topic">
    ...
</div> <!-- end of "topic" -->
<!-- frame of MetaContent -->
<div id="metacontent">
    ...
</div> <!-- end of "MetaContent" -->
app.get('/xyz', function(req, res){
    res.render('index.ejs', { title: 'My Site' });
});
app.get('/contact', function(req, res){
        res.render('contact.ejs', { title: 'My Site' });
    });
app.get('/xyz/contact', function(req, res){
        res.render('contact.ejs', { title: 'My Site' });
    });