简体   繁体   中英

using handlebar template with moment

I use moment and handlebar. I would like to use moment in handelbar.

I create a file with the content of this page http://moment.handlebars.solidgoldpig.com/handlebars.moment.js.html

Git hub project https://github.com/solidgoldpig/handlebars.moment

So the content of my handlebars.moment.js is http://pastebin.com/vhpg4SHA

In my code, i have

<script src="js/handlebars.min.js" type="text/javascript" ></script>
<script src="js/handlebars.moment.js" type="text/javascript" ></script>

In my template i have

{{moment birthdate "L"}}

I get the error

Uncaught Error: Missing helper: "moment"

I don't use nodejs, is there a specific thing for nodejs? is there a way to use side on the client side without node js?

It looks like the Moment helpers for Handlebars has some requirements of it's own:

  1. A CommonJS or AMD module loader
  2. Lodash and moment (and of course Handlebars)

To solve these requirements, you are probably best off using a bundler such as Browserify or Webpack . They bundle all your dependencies and your dependencies' dependencies without worrying about it yourself (too much) based on your application code.

Further, you need to register the moment helper with Handlebars , as shown in the code snippet on http://moment.handlebars.solidgoldpig.com/index.html , ie:

var Handlebars = require("handlebars");
var MomentHandler = require("handlebars.moment");
MomentHandler.registerHelpers(Handlebars);  // <- this is the important line

If you don't want to include a bundler, you need to include all dependencies manually and also make some adjustments to the module factory of the module you're trying to include.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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