gpt4 book ai didi

node.js - 通过 Consolidate 和 Mustache 在 Express 中使用布局 View

转载 作者:搜寻专家 更新时间:2023-11-01 00:46:28 25 4
gpt4 key购买 nike

我刚开始使用 Express 浏览 Node,最后让 Consolidate JS 正常工作,按照 Consolidate JS Github page 上的说明使用 Mustache 作为模板 View 系统。 .

Mustache 加载正确,但我现在想知道如何在模板呈现中包含布局文件。默认的 Jade 系统加载 layout.jade 文件中 .render 方法的内容。我只是想知道如何使用 Mustache 做同样的事情。非常感谢任何帮助!

代码:

index.js

 exports.index = function(req, res){
res.render('index', { title: "Work pl0x?" });
});

index.mustache

欢迎来到{{title}}

我只希望 index.mustache 内容出现在下面代码的“{{content}}”部分 (layout.mustache)。我该怎么做?

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" />
<title>Project Name | {{title}}</title>
<link href="/stylesheets/style.css" rel="stylesheet" />
</head>

<body>

{{content}}

</body>
</html>

最佳答案

获得这种行为(某种)的一种方法是:

exports.index = function(req, res){
res.render('layout', { title: "Work pl0x?", partials: { content: "index" });
});

然后在你的layout.html中这样写

<body>
{{>content}}
</body>

关于node.js - 通过 Consolidate 和 Mustache 在 Express 中使用布局 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12209607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com