gpt4 book ai didi

javascript - 将 Handlebars 表达式转换为小写和破折号

转载 作者:行者123 更新时间:2023-11-30 12:45:51 26 4
gpt4 key购买 nike

我正在尝试重用我的 handlebars 中的一些数据模板在前端。

为此,我需要将其中一个表达式转换为小写,并使用破折号代替空格。这很容易做到吗?

例如,在我的 JSON 文件中有以下内容:

var items = [{
id: '1',
title: "Item Number One",
},

{
id: '2',
title: "Item Number Two",
},

];

在前端的 Handbars 模板上,我想这样做:

<script type="text/x-handlebars" id="items">

<h1>
{{title}}
</h1>

<img src="{{unbound title}}.png" />

</script>

这会将图像页面输出为 <img src="Item Number One.png" />这是不好的。那么有没有一种简单的方法可以将其转换为小写并删除空格?

最佳答案

制作自定义 Handlebars Helper:

Handlebars.registerHelper("noSpaces", function(input) {
var output = input.toLowerCase();
return output.replace(" ", "");
});

然后将其命名为:

<img src="{{noSpaces title}}.png" />

关于javascript - 将 Handlebars 表达式转换为小写和破折号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22510867/

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