gpt4 book ai didi

node.js - 如何在 hapi.js ejs 布局中获取多个内容区域?

转载 作者:太空宇宙 更新时间:2023-11-04 00:22:42 25 4
gpt4 key购买 nike

我想在模板中定义信息,该模板使用ejs在包含布局中呈现。在 hapi.js 中。例如:

layout.html

<html>
<head>
<title><%- contentFor('title') %></title>
</head>
<body>
<%- content %>
</body>
</html>

index.html

contentFor('title', 'My title')

<h1>My content</h1>

对我来说重要的是,无论它如何工作,不同的布局内容都是在模板中定义的,而不是在路由级别传递的。这可能吗?

最佳答案

是的,这根本不明显。我能够通过以下方式做到这一点:

  1. 设置默认上下文对象
  2. 修改模板中的该对象
  3. 在布局中引用该对象的属性

例如:

设置默认上下文对象

server.register(plugins, (err) => {
...
server.views({
context: {
layoutContent: {}
}
})
}

修改模板中的对象

// templates/my_template.html
<%
layoutContent = {
title: 'My Title'
meta: '<meta name="description" content="my content">'
}
%>

引用布局中的对象

// layouts/layout.html
<html>
<head>
<title><%- layoutContent.title %></title>
<%- layoutContent.meta %>
</head>
<body>
<%- content %>
</body>
</html>

如您所见,您可以将文本或整个标记传递到布局。希望这会有所帮助。

关于node.js - 如何在 hapi.js ejs 布局中获取多个内容区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43985167/

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