Mustache JS 和单数/复数

发布于 2024-12-15 19:09:52 字数 275 浏览 0 评论 0原文

我使用 Mustache 来模板化我的 javascript ajax 调用,这是我的数据和模板:

{'joined':1} // ajax responde data json.

var myTemplate = '{{ joined }} person joined so far.'

它有效,但是我想修复其中的语法,如果超过 1 人加入,我想显示到目前为止有 5 人加入。

如何在不操作服务器端 ajax json 响应器的情况下实现这一目标?

I use mustache for templating my javascript ajax calls, Here is my data and the template:

{'joined':1} // ajax responde data json.

var myTemplate = '{{ joined }} person joined so far.'

It works, however I want to fix the grammer in this, if more than 1 person joins, I want to show 5 people joined so far.

How to achieve this without manipulating the server side ajax json responder ?

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

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

发布评论

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

评论(2

我最亲爱的 2024-12-22 19:09:52

如果您可以诱导服务器端 AJAX 以这种方式传递它,则可以在 JavaScript 对象内添加条件逻辑:

var json = {
    'joined': 1,
    'ppl': function() {
        return (this.joined === 1) ? 'person' : 'people'
    }
} // ajax responde data json.
var myTemplate = '{{ joined }} {{ppl}} joined so far.'

Mustache.to_html(myTemplate, json);

http ://jsfiddle.net/mblase75/H8tqn/

You can add conditional logic inside the JavaScript object, if you can coax your server-side AJAX into delivering it that way:

var json = {
    'joined': 1,
    'ppl': function() {
        return (this.joined === 1) ? 'person' : 'people'
    }
} // ajax responde data json.
var myTemplate = '{{ joined }} {{ppl}} joined so far.'

Mustache.to_html(myTemplate, json);

http://jsfiddle.net/mblase75/H8tqn/

苏大泽ㄣ 2024-12-22 19:09:52

实际上,您可以仅使用 Mustache 来完成此操作,但对于这种情况,您的 JSON 不仅包含数字,还包含值数组以及数组的大小:

var json = {
  'rows': ['a','b','c'],
  'numRows': function() {
      return this.rows.length
   }
} // ajax response data json.

为此,您可以使用以下 Mustache 模板之一:

在简单的情况下,当您只需要为复数添加“s”时:

var myTemplate = '{{ numRows }} link{{^rows}}s{{/rows}}{{#rows.1}}s{{/rows.1}} parsed so far.'

结果:

0 links parsed so far.
1 link parsed so far.
2 links parsed so far.

在一般情况下,当复数是特殊的(如 people/person)时:

var myTemplate2 = '{{ numRows }} {{^rows}}people{{/rows}}{{#rows.0}}{{^rows.1}}person{{/rows.1}}{{/rows.0}}{{#rows.1}}people{{/rows.1}} joined so far.'

结果:

0 people joined so far.
1 person joined so far.
2 people joined so far.

您可以在此处找到在实践中应用的两个模板:
http://jsfiddle.net/H8tqn/9/


PS如果可以的话我会在这里再次发帖找到这个问题的解决方案:

No people joined so far.
1 person joined so far.
2 people joined so far.

Actually you can do it with only Mustache, but for the case your JSON contains not only a number, but an array of values, together with the size of the array:

var json = {
  'rows': ['a','b','c'],
  'numRows': function() {
      return this.rows.length
   }
} // ajax response data json.

In order to do it, you can use one of the following Mustache templates:

In the simple case, when you just need to add "s" for plural:

var myTemplate = '{{ numRows }} link{{^rows}}s{{/rows}}{{#rows.1}}s{{/rows.1}} parsed so far.'

The result:

0 links parsed so far.
1 link parsed so far.
2 links parsed so far.

In the general case, when the plural is special (like people/person):

var myTemplate2 = '{{ numRows }} {{^rows}}people{{/rows}}{{#rows.0}}{{^rows.1}}person{{/rows.1}}{{/rows.0}}{{#rows.1}}people{{/rows.1}} joined so far.'

The result:

0 people joined so far.
1 person joined so far.
2 people joined so far.

You can find both templates applied in practice here:
http://jsfiddle.net/H8tqn/9/


P.S. I will post again here if I can find the solution for this one:

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