简体   繁体   中英

Can I compile loaded handlebars templates?

Is there a way for me to compile Handlebars templates that are loaded as individual script tags with a src attribute? For example, if I have

<script id="index-template" type="text/x-handlebars-template" src="templates/index.hbs">

I know I can precompile them , but I cannot introduce this step as it requires npm + my company doesn't use it.

I tried to find some way to use jQuery to get the source of the loaded file, but couldn't figure it out. Also, I know I can use AJAX, but I'm looking for another solution.

Precompilation requires Node.js. If you can't have it installed on every developer's machine, you may create a service for others to use (I can't find any existing one).

If you want to compile templates run-time, you can get them using AJAX. If the file have already been downloaded, it will be taken from cache. It is still an async operation, so you may use the same syntax for processing the result.

Funnily enough, I just wrote a Handlebars precompile bundler today to use with the asp.net optimization library. It takes advantage of Jurassic, as is explained here .

I have yet to put it in a github repo, but it will be once I make it slightly more generic. I will edit the post and point to the repo later tonight if anyone is interested.

It does take dependencies on Jurassic as well as System.Web.Optimization 1.1.

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