express - 如何在 Handlebars 中显示从 express js 传输的数据?
问题描述
我用express js中的render函数发送数据,数据的形式
[
{
id_customers: 1,
name_company: 'dsfsd',
nip: 23423,
adres: 'dsfsd',
phone_number: '23423',
createdAt: 2019-12-31T23:00:00.000Z,
updatedAt: 2019-12-31T23:00:00.000Z
},
{
id_customers: 2,
name_company: 'nokia',
nip: 23232323,
adres: 'dfsd',
phone_number: '23123123',
createdAt: 2019-12-31T23:00:00.000Z,
updatedAt: 2019-12-31T23:00:00.000Z
}
]
我如何在车把上显示这些?
解决方案
这是一个使用handlebars
with express
and的示例express-handlebars
:
https ://codesandbox.io/s/display-datas-handlebars-pftcs
index.js
var express = require("express");
var exphbs = require("express-handlebars");
var app = express();
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
app.get("/", function (req, res) {
res.render("home", {
customers: [
{
id_customers: 1,
name_company: "dsfsd",
nip: 23423,
adres: "dsfsd",
phone_number: "23423"
},
{
id_customers: 2,
name_company: "nokia",
nip: 23232323,
adres: "dfsd",
phone_number: "23123123"
}
]
});
});
app.listen(8080);
视图/布局/main.handlebars
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example App</title>
</head>
<body>
{{{body}}}
</body>
</html>
视图/home.handlebars
<ul class="people_list">
{{#each customers}}
<li>{{this.name_company}} - {{this.adres}}</li>
{{/each}}
</ul>
推荐阅读
- php - 在前缀根路径中找不到路由
- kotlin - 在 Kotlin 中覆盖 Java final 函数的 Kotlin 编译器问题
- python - 从包含数组的不同列之间的匹配元素创建一个新列
- pyspark - 如何在 `groupBy()` 之后选择 DataFrame 的特定行?
- python - 注释不存在的 dict 字段不应该产生错误吗?
- c# - 在 WPF 应用程序中包含字体
- ios - UINavigationBar(嵌入在 UIImagePickerController 中)背景颜色未更新仅在 iOS13 中发生的此问题
- android - kotlin 是否有用于从两种不同类型的列表中获取公共数据的高阶函数?
- javascript - 如何在axios中添加拦截器功能?
- ios - 当我从 Scheme Url(Swift 项目)启动应用程序时未调用 openURL