javascript - 使用带矩的 Handlebars 模板

标签 javascript node.js momentjs handlebars.js

我使用力矩和 Handlebars 。我想在handelbar 中使用moment。

我创建一个包含此页面内容的文件 http://moment.handlebars.solidgoldpig.com/handlebars.moment.js.html

Git 中心项目 https://github.com/solidgoldpig/handlebars.moment

所以我的handlebars.moment.js的内容是http://pastebin.com/vhpg4SHA

在我的代码中,我有

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

在我的模板中,我有

{{moment birthdate "L"}}

我收到错误

Uncaught Error: Missing helper: "moment"

我不使用nodejs,nodejs有什么具体的东西吗?有没有办法在没有 Node js 的情况下在客户端使用 side ?

最佳答案

看起来 Handlebars 的 Moment 助手有一些自己的要求:

  1. CommonJS 或 AMD 模块加载器
  2. Lodash和力矩(当然还有 Handlebars )

要解决这些要求,您可能最好使用 bundler ,例如 BrowserifyWebpack 。他们根据您的应用程序代码捆绑您的所有依赖项以及依赖项的依赖项,而无需您自己(太多)担心。

此外,您需要向 Handlebars 注册 moment 帮助器,如 http://moment.handlebars.solidgoldpig.com/index.html 上的代码片段所示,即:

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

如果您不想包含捆绑程序,则需要手动包含所有依赖项,并对您尝试包含的模块的模块工厂进行一些调整。

关于javascript - 使用带矩的 Handlebars 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863553/

相关文章:

node.js - 对某些案例问题的警告 computedMatch 使用react?

javascript - 在 Mongoose 中推送 ObjectId

javascript - 如何在客户端使用estraverse js库?

Angular Material 2 - MatDatePicker 错误 - 'Must import MatNativeDateModule/MatMomentDateModule'

php - 包含 js 库以从正文内部开始,并且仅特定于一页

javascript - 将选定的 textContent 和新值传递给更新函数时,元素文本不会更改

javascript - Jade 重音编码问题

javascript - 如何从这两个可观察对象中只创建一个订阅?

momentjs - Moment js 未按预期工作

javascript - momentjs isValid 在应该为 true 时返回 false,反之亦然