简体   繁体   English

使用自定义帮助器预编译车把模板

[英]Precompiling handlebars template with custom helpers

I have a message.handlebars template which works perfect when compiling it in the browser. 我有一个message.handlebars模板,在浏览器中进行编译时可以完美工作。

I precompiled it with handlebars -message.handlebars -f -message.js successfuly. 我成功地使用handlebars -message.handlebars -f -message.js对其进行了预编译。

Then in my ASP.NET MVC site I'm adding the references as: 然后在我的ASP.NET MVC网站中,将引用添加为:

<script src="~/Scripts/handlebars.runtime-v1.3.0.js"></script>
<script src="~/Scripts/handlebars-helpers.js"></script>
<script src="~/Scripts/templates/message.js"></script>

The handlebars-helpers.js contains some custom helpers used by the message template. handlebars-helpers.js包含消息模板使用的一些自定义帮助器。

I took this approach from this guy's comment , but it's not working. 我从这个人的评论中采用了这种方法,但是它没有用。 I'm getting an error when doing var html = Handlebars.templates.message(data); 在执行var html = Handlebars.templates.message(data);时出现错误var html = Handlebars.templates.message(data); . Again this works perfectly when compiling it in the browser, so it's not a problem of context. 同样,在浏览器中进行编译时,这非常完美,因此这不是上下文的问题。

The error I'm getting is: 我得到的错误是: 在此处输入图片说明

Is this the correct approach to precompile and use custom helpers? 这是预编译和使用自定义助手的正确方法吗? Or is there a way to include those helpers in the precompiled template? 还是有办法在预编译的模板中包含这些助手?

It was the version of handlebars on the server. 它是服务器上把手的版本。 NPM installs the latest by default and the version 2 alpha was causing the issue. NPM默认情况下安装最新版本,而版本2 Alpha导致了该问题。 Found this fix on Github ( https://github.com/wycats/handlebars.js/issues/734 ): 在Github( https://github.com/wycats/handlebars.js/issues/734 )上找到此修复程序:

npm uninstall handlebars -g
npm install handlebars@1.3 -g

Everything working! 一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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