简体   繁体   English

车把帮手和.toLocaleString

[英]Handlebar helper and .toLocaleString

I display results of a json in Handlebars including numbers that I'd like to format with .toLocaleString(). 我在车把中显示json的结果,其中包括我想使用.toLocaleString()格式化的数字。 My json sample: 我的json样本:

[{"id": "1",
 "results": [{
     "price": 5000
      },
      {
     "price": 6000
  }]
  },
  {"id": "2",
 "results": [{
     "price": 15000
      },
      {
     "price": 16000
      }]
  }]...

Then I render it with Express in the backend and Handlebars in front: 然后使用后端的Express和前面的Handlebars渲染它:

{{#each results as |auction|}}                    

     <span class="results__container--price">{{auction.price}} €</span>
{{/each}}

(In reality, it's more complexe) (实际上,它更复杂)

But I don't know how to use toLocaleString(). 但是我不知道如何使用toLocaleString()。 Maybe I have to loop through "prices in Express but I don't know how to do it. Is it possible to use it directly with Handlebars ? 也许我必须遍历“ Express中的价格,但我不知道该怎么做。是否可以将其直接与Handlebars一起使用?

Use a helper function 使用辅助功能

 Handlebars.registerHelper('toLocaleString', function(number) { return number.toLocaleString() }) var source = document.getElementById("temp").innerHTML; var template = Handlebars.compile(source); var context = { title: "Hello", count: 123456789 }; var html = template(context); document.getElementById("out").innerHTML = html; 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.1.2/handlebars.min.js"></script> <script id="temp" type="text/x-handlebars-template"> <div class="test"> <h1>{{title}}</h1> <h2>{{toLocaleString count}} </div> </script> <div id="out"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM