如何将 handlebars 参数从字符串部分转换为整数?
How convert handlebars param from string to integer in partial?
我正在用 {{> my-partial width="1200" }}
调用部分所以,在那个部分我需要使用 width
参数来进行像 {{divide (toInt width) 2}}
这样的算术运算(以获得 600) 但我在 divide
帮助程序中收到以下错误:
(node:5185) UnhandledPromiseRejectionWarning: TypeError: expected the first argument to be a number
我正在使用 gulp 和 assemble,同时,我正在使用 math 和 number 把手助手。
有什么建议我该如何处理?
您可以制作自己的助手。
转到 app.js(或任何应用程序入口点)并添加以下行:
var hbs = require('hbs');
hbs.registerHelper('_toInt', function(str) {
return parseInt(str,10);
});
我正在用 {{> my-partial width="1200" }}
调用部分所以,在那个部分我需要使用 width
参数来进行像 {{divide (toInt width) 2}}
这样的算术运算(以获得 600) 但我在 divide
帮助程序中收到以下错误:
(node:5185) UnhandledPromiseRejectionWarning: TypeError: expected the first argument to be a number
我正在使用 gulp 和 assemble,同时,我正在使用 math 和 number 把手助手。
有什么建议我该如何处理?
您可以制作自己的助手。 转到 app.js(或任何应用程序入口点)并添加以下行:
var hbs = require('hbs');
hbs.registerHelper('_toInt', function(str) {
return parseInt(str,10);
});