mustache.js日期格式

Doo*_*oie 8 string json date-parsing mustache

我已经开始使用mustache.js,到目前为止我印象非常深刻.虽然有两件事让我困惑.第一个引导到第二个跟我一起.

我的JSON

{"goalsCollection": [
    {
        "Id": "d5dce10e-513c-449d-8e34-8fe771fa464a",
        "Description": "Multum",
        "TargetAmount": 2935.9,
        "TargetDate": "/Date(1558998000000)/"
    },
    {
        "Id": "eac65501-21f5-f831-fb07-dcfead50d1d9",
        "Description": "quad nomen",
        "TargetAmount": 6976.12,
        "TargetDate": "/Date(1606953600000)/"
    }
]};
Run Code Online (Sandbox Code Playgroud)

我的处理功能

function renderInvestmentGoals(collection) {
    var tpl = '{{#goalsCollection}}<tr><td>{{Description}}</td><td>{{TargetAmount}}</td><td>{{TargetDate}}</td></tr>{{/goalsCollection}}';
    $('#tblGoals tbody').html('').html(Mustache.to_html(tpl, collection));
}
Run Code Online (Sandbox Code Playgroud)

Q1 你可以看到我的'TargetDate'需要解析,但我不确定如何在我当前的函数中做到这一点.

Q2 说我想在渲染之前对一个或多个对象执行某些功能或格式化,这样做的最佳方法是什么?

McG*_*gle 19

你可以使用"lambda"

"TargetDate": "/Date(1606953600000)/",
"FormatDate": function() {
    return function(rawDate) {
        return rawDate.toString();
    }
}, ...
Run Code Online (Sandbox Code Playgroud)

然后在标记中:

<td>
{{#FormatDate}}
    {{TargetDate}}
{{/FormatDate}}
</td>
Run Code Online (Sandbox Code Playgroud)

从链接:

当值是可调用对象(例如函数或lambda)时,将调用该对象并传递文本块.传递的文本是文本块,未经渲染.


JVi*_*ela 9

我为Mustache.js创建了一个小扩展,它允许在表达式中使用格式化程序,如{{expression | 格式}}

无论如何,你需要创建一个解析日期值的函数,如下所示:


      Mustache.Formatters = {
        date: function( str) {
          var dt = new Date( parseInt( str.substr(6, str.length-8), 10));
          return (dt.getDate() + "/" + (dt.getMonth() + 1) + "/" + dt.getFullYear());
        }
      };
Run Code Online (Sandbox Code Playgroud)

然后只需将格式化程序添加到表达式中:

{{TargetDate | date}}
Run Code Online (Sandbox Code Playgroud)

你可以从这里获取代码:http://jvitela.github.io/mustache-wax/


HMR*_*HMR 6

这是很久以前的事了,但是在寻找完全相同的东西.Mustachejs(现在)允许您调用传递数据的函数,而不仅仅是那个; 在函数中,值this是一个部分中的任何值都是真的.

如果我的模板是这样的:

{{#names}}
    <p>Name is:{{name}}</p>
    <!-- Comment will be removed by compileTemplates.sh
         #lastLogin is an if statement if lastLogin it'll do this 
         ^lastLogin will execute if there is not lastLogin      
    -->
    {{#lastLogin}}
    <!-- 
      formatLogin is a method to format last Login 
      the function has to be part of the data sent 
      to the template
    -->
    <p>Last Login:{{formatLogin}}</p>
    {{/lastLogin}}
    {{^lastLogin}}
    not logged in yet
    {{/lastLogin}}
    {{#name}}
     passing name to it now:{{formatLogin}}
    {{/name}}
{{/names}}
Run Code Online (Sandbox Code Playgroud)

和这样的数据:

var data={
    names:[
        {name:"Willy",lastLogin:new Date()}
    ],
    formatLogin:function(){
          //this is the lastDate used or name based on the block
                  //{{#name}}{{formatLogin}}{{/name}}:this is name
                  //{{#lastLogin}}{{formatLogin}}{{/lastLogin}}:this is lastLogin
          if(!/Date\]$/.test(Object.prototype.toString.call(this))){
              return "Invalid Date:"+this;
          }
          return this.getFullYear()
            +"-"+this.getMonth()+1
            +"-"+this.getDate();
    }
};
var output = Mustache.render(templates.test, data);
console.log(output);
Run Code Online (Sandbox Code Playgroud)


mys*_*ory -2

我也一直在我的项目中使用 Mustache,因为它能够跨客户端/服务器共享。我最终所做的是将所有值(日期、货币)格式化为服务器端字符串,因此我不必依赖辅助 Javascript 函数。但是,如果您在客户端针对这些值执行逻辑,那么这可能不太适合您。

您可能还想考虑使用handlebars.js,它本质上是Mustache,但具有可能有助于客户端格式化(以及更多)的扩展。这里的损失是,如果这对您很重要,您可能无法找到车把的服务器端实现。

  • 这是一个糟糕的答案。数字的格式是表示逻辑,而不是业务或应用程序逻辑。它不属于服务器。相反,它应该以某种方式成为模板语言或 GUI 的一部分。 (13认同)