GRMustache 格式的数字,或 iOS 中具有数字格式的 HTML 模板引擎

发布于 2024-12-27 20:41:39 字数 507 浏览 5 评论 0原文

我不知道如何最好地解决这个问题。我尝试用自己的方式解决这个问题,但到目前为止失败了 。我尝试使用 GRMustache,却发现我正在尝试显示在我尝试使用的模板中看起来很丑陋的浮动。

基本上,我有一个模型,我试图通过模板将其输出为 HTML。理想情况下,我只需将变量名称/键路径放入模板中,模板就会使用渲染到位的实际值(几乎)进行解析。但是,我使用的模型使用浮点数进行所有计算,我真的希望它们呈现为逗号分隔的整数字符串(例如 (float)9382.233325 => "9,382" )。

我似乎在 GRMustache 中找不到任何涵盖此类情况的文档,但我想这不会是一个不常见的要求。有谁知道如何使用 GRMustache 或通过其他技术来做到这一点?

I'm not sure how to best go about this. I've tried solving this my own way, but failed so far. I tried using GRMustache, only to realize that I'm trying to display floats that just look hideous in the template I'm trying to use.

Basically, I have a model that I'm trying to have output as HTML via a template. Ideally, I just put the variable names/keypaths into the template and the template just gets parsed with the actual values (pretty much) rendered in place. But, the model I'm using uses floats for all its calculations and I'd really like them rendered as comma-separated integer strings (e.g. (float)9382.233325 => "9,382").

I can't seem to find any documentation in GRMustache that covers a situation like this, but I imagine this can't be an uncommon requirement. Does anyone know how to do this with GRMustache or through some other technique?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕梦 2025-01-03 20:41:39

我是《GRMustache》的作者。

GRMustache 中没有,也永远不会有任何浮动格式化功能,因为操作系统中已经有一个非常适合的工具:NSNumberFormatter。

由于您将模型对象提供给 GRMustache,因此我的建议是:

在模型上声明一个类别,并为每个格式化值添加特定方法:

@interface MYModel(GRMustache)
// name would match your original value property name
- (NSString *)formattedValue;
@end

在实现文件中,使用 NSNumberFormatter:

@implementation MYModel(GRMustache)
- (NSString *)formattedValue
{
  // Check the NSNumberFormatter reference for initializing
  // the NSSNumberFormatter for your desired output.
  NSNumberFormatter *formatter = [NSSNumberFormatter ...]; 
  return [formatter stringFromNumber: [self value]];
}
@end

注意创建许多 NSNumberFormatter 实例可能成本高昂。一种好的做法是提供一种返回共享方法的共享方法。上面的代码只是该技术的提示。

最后,在模板中,将 {{value}} 标记替换为 {{formattedValue}}

快乐GR小胡子!

I'm the author of GRMustache.

There isn't, and there will never be any float formatting features in GRMustache, because there is already a perfectly well suited tool in the OS: NSNumberFormatter.

Since you're giving to GRMustache your model objects, here is my advice:

Declare a category on your model, and add a specific method for each of your formatted value:

@interface MYModel(GRMustache)
// name would match your original value property name
- (NSString *)formattedValue;
@end

In the implementation file, use a NSNumberFormatter:

@implementation MYModel(GRMustache)
- (NSString *)formattedValue
{
  // Check the NSNumberFormatter reference for initializing
  // the NSSNumberFormatter for your desired output.
  NSNumberFormatter *formatter = [NSSNumberFormatter ...]; 
  return [formatter stringFromNumber: [self value]];
}
@end

Beware creating many NSNumberFormatter instances may be costly. A good practice is to provide a shared method that returns a shared one. The code above is just a hint for the technique.

Finally, in your template, replace {{value}} tags with {{formattedValue}}.

Happy GRMustache!

清欢 2025-01-03 20:41:39

GRMustache 1.12 现在提供了更好的数字格式化 API:https:// github.com/groue/GRMustache/blob/master/Guides/sample_code/number_formatting.md

GRMustache 1.12 now features a better API for number formatting: https://github.com/groue/GRMustache/blob/master/Guides/sample_code/number_formatting.md

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文