我可以测试一个字符串是否在handlebars中包含另一个字符串吗?而不注册一个手把手助手?
这就是我现在的代码:
{{#eq message "specialMarker"}}
<p>some other text</p>
{{/eq}}
如果message
字符串包含"special marker"
,我想返回“其他一些文本
发布于 2021-08-13 09:24:17
你需要创建一个像这样的帮助器:
Handlebars.registerHelper("eq", function (options) {
if (options.fn(this).includes("specialMarker")) {
return "<p>some other text</p>";
}
return "<p>Not included</p>";
});
hbs
文件将为:
{{#eq}}{{message}}{{/eq}}
Codesandbox中的示例
https://stackoverflow.com/questions/68730786
复制相似问题