问题标签 [express-handlebars]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
187 浏览

handlebars.js - 用 Handlebars 循环替换 Jade for ...

我需要将迭代状态并将状态分配给帐户的 Jade 选择列表转换为 Handlebars 模板。在 Jade 中,它看起来像这样:

我试图这样#each

但它不起作用,因为它不是一个数组。

有没有办法用把手做到这一点?谢谢你。

0 投票
1 回答
162 浏览

express - 在 express/handlebars/mongoose 中计算某个值的对象的属性

在 express/handlebars/mongoose 中创建好友系统。我正在使用 {{friends.length}} 来显示用户拥有的朋友总数,但我将如何显示朋友的总类型。例如。

其中有多少朋友的状态为 0 或 1。

0 = 待处理 1 = 朋友

如何通过 mongo/mongoose 获取每个的计数?

0 投票
1 回答
694 浏览

javascript - 无法从车把助手中检测到当前语言(i18next.language)?

我正在为多语言解决方案使用以下软件包。

var i18next = 需要('i18next');

var i18nFsBackend = require('i18next-node-fs-backend');

var i18nMiddleware = require('i18next-express-middleware');

由于我使用把手作为我的 nodejs 模板引擎,所以我不能直接在 HTML 中使用 i18next t('key') 。所以我创建了一个车把助手,如下所示

但是,问题是该功能无法检测到语言已更改

我的解决方法

应用程序.js

路线

如您所见,在路线上我分配 res.app.locals.language = req.language; 然后在车把辅助函数中,我使用 app.locals.language 获取当前语言并使用 i18next.changeLanguage() 更改语言。

它奏效了。

我想知道我做的对不对?或者如果有更好的解决方案

0 投票
0 回答
186 浏览

javascript - 如何使用把手渲染和迭代 JSON

我有一个名为 twitData.json 的 JSON 文件,如下所示: JSON文件

我想使用 Handlebars 和 Express 对其进行迭代,并将其传递给要实现为 html 代码的部分。我已将 JSON 文件包含在 server.js 中。我在 server.js 中使用 res.render 来尝试将 JSON 的元素传递给部分。像这样: res.render('twitPage', {twitData}) 。对于我的车把文件,我使用

{{each .}}
{{twit}}
{{/each}}
我正在使用的部分 twit.handlebars 看起来像这样。它需要两个参数。我应该使用 res.render 以某种方式传递参数。

在此处输入图像描述

我不明白的主要部分是在 res.render 中作为第二个参数放置什么,以及我应该在 {{each}} 帮助器中放置什么作为上下文。任何想法,将不胜感激。如果您需要更多信息,我可以提供。

这是 twitPage.handlebars:

在此处输入图像描述

这是 server.js: 在此处输入图像描述

0 投票
2 回答
454 浏览

javascript - Nodejs Express 车把发布 404 错误“/checkout”

我正在使用 Node、Express、handlebars 和 MongoDB 构建一个购物车项目。目前,当我尝试使用 Jquery 提交表单时,我的“/checkout”文件收到 404 错误。最初,我使用return false来防止表单在收到数据之前提交。为了解决这个问题,我也尝试使用event.preventDefault。在另一个论坛上,有人建议我的crsf保护没有正确创建令牌。我使用该路线并添加到我的视图中,最后添加到hbs表单中。这些尝试似乎都没有解决我的问题。

这是我的要点的链接。https://gist.github.com/Satellite9/5e4ce3de5c19cee2f355d872b6d7d3c8

这是我的浏览器发送给我的错误。

*错误:在 C:\Users\Leimamo\PhpstormProjects\untitled2\app.js:60:13 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\ router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\ untitled2\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 在下一个 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index .js:635:15 在下一个(C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:260:14) 在 Function.handle (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:174:3) 在路由器 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\ express\lib\router\index.js:47:12) 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5)在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\ index.js:284:7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 在下一个 (C:\Users\Leimamo\PhpstormProjects \untitled2\node_modules\express\lib\router\index.js:275:10) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\app.js:51:5 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2 \node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C :\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12)在下一个(C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index. js:275:10) 在 serveStatic (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\serve-static\index.js:75:16) 在 Layer.handle [as handle_request] (C:\Users\Leimamo\ PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:284 :7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 在下一个 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\ express\lib\router\index.js:275:10) 在 SessionStrategy.strategy.pass (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\passport\lib\middleware\authenticate.js:325:9) *12) 在下一个 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 SessionStrategy.strategy.pass (C:\Users\Leimamo\PhpstormProjects\untitled2\ node_modules\passport\lib\middleware\authenticate.js:325:9) *12) 在下一个 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 SessionStrategy.strategy.pass (C:\Users\Leimamo\PhpstormProjects\untitled2\ node_modules\passport\lib\middleware\authenticate.js:325:9) *

这是npm给我的错误 Post /checkout 404 206.855 ms -5424

感谢您提供的任何见解。

基隆

0 投票
2 回答
1649 浏览

node.js - 使用 express 框架在车把中渲染数据

您好,我在 Express Framework 中工作,我正在使用把手来渲染我的 mysql 表中的数据。在尝试使用下面的代码呈现数据时,而不是呈现值它显示 [object object]。我在下面发布了我的代码。

index.js:

请求表.hbs:

表单输入中的值显示为 [object object]。我尝试作为 data.key_value 呈现数据,但它没有显示值。请给我一个解决方案。谢谢你。

0 投票
1 回答
11513 浏览

handlebars.js - 如何使用带有 nodemailer 的车把发送电子邮件?

我正在使用 nodemailer 使用以下nodemailer-express-handlebars插件发送电子邮件。我用这个 {dead blog post} 作为参考

代码正在编译welcome模板但未使用layout

我的代码如下:

layout.handlebars有以下代码

0 投票
1 回答
487 浏览

node.js - 自定义 Express 车把 If Helper

我正在迭代一个数组对象express-handlebars,并且只有每个创建的第二个 dom 元素应该具有特定的属性。

像这样的东西:

我注意到有一个@size包含迭代索引的值。

到目前为止,我的代码如下所示:

但是如何在 if 语句中添加一个条件,该条件只对true每个第二项进行评估?

我一直在尝试实现此功能,这是我目前的方法:

0 投票
1 回答
902 浏览

express - Express & res.render 将模板作为字符串传递

我想执行 res.render,但不是像这样将模板文件作为参数传递:

我希望能够将模板作为这样的字符串传递:

上面的代码显然不起作用,因为 res.render 只接受文件路径/名称。关于如何实现这一目标的任何想法?

0 投票
1 回答
286 浏览

javascript - Detect changed input Values of an HTML input table

Im trying to findout how i can Detect changed Values in the Following prefilled input Table. I get the Data from my MongoDB and use Express-Handlebars and nodejs to fill in the data to the HTML.

When i click the Submit button the server will recieve An object with all the configs . Which i then want to write back to the MongoDB. But I dont want to write unchanged Values back to it. I could iterate over the Object but as the Rows are basically unlimited that could lead to Server perfomance problems.

I wanted to know if there is away to detect if a value has changed in that specific row and mark it somehow in the Object my server recieves. That would allow me to just Iterate once over the whole Object and only write those Configs that changed.

I saw that there is some kind of onChanged thing in HTML or JS that could do that. But i only saw it beeing used for direct changes and I only want changes to be applied when the Button is pressed.

I would like to avoid Javascript tho if possible!

I hope I could somehow express what I want to achieve. Thanks in advance!