简体   繁体   中英

Precompiling handlebars template with custom helpers

I have a message.handlebars template which works perfect when compiling it in the browser.

I precompiled it with handlebars -message.handlebars -f -message.js successfuly.

Then in my ASP.NET MVC site I'm adding the references as:

<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.

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); . 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. Found this fix on Github ( https://github.com/wycats/handlebars.js/issues/734 ):

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

Everything working!

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