Using functions in mustache.js in express 3.0

I'm new to mustache.js and using it in express 3.0 in node.js and I want to use a helper function for formatting time in templates.

In app.js:

app.locals({
  helper: {
    friendly_time: require('./libs/util').friendly_time
  }
});

friendly_time is a function function friendly_time(date){ ... }, date is a Date object.

In index.hjs:

{{#articles}}
  <div class="org-articles">
    <ul class="org-articles-list">
      <li class="org-articles-item">
        <a class ="org-article-title" href="/o/{{title}}">
        {{title}}
        </a>&nbsp;-&nbsp;
        <span class="org-article-mtime">
          {{#helper.friendly_time}}
            {{mtime}}
          {{/helper.friendly_time}}
        </span>
      </li>
    </ul>
  </div>
{{/articles}}

articles is an array containing objects like:

{
    title: 'title',                           // a string
    mtime: new Date() // a js Date object
}

And then the process throw error:

TypeError: Object {{mtime}} has no method 'getFullYear'

I know articles[i].mtime is treated as a string {{mtime}} and that causes the error. But I don't know how correct this. Any help is appreciated.

I think you need to use three braces so that mustache doesn't try to clean the input data. Though I'm not positive what it does with Date objects.

{{#helper.friendly_time}}
    {{{mtime}}}
{{/helper.friendly_time}}

The other option is to update friendlyTime so that it can also accept a string and convert it into a date object before attempting to call getFullYear.

function friendly_time(date){
    if(!date.getFullYear) {
        date = new Date(date);
    }

    ...
}