且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在使用Handlebars.js时将模板js外包给不同的文件

更新时间:2023-10-25 21:26:40

使用jQuery:

  $。get(user_template.js,function(template_text){
var template = Handlebars.compile(template_text);
// more things
});


I have this template script

<script id="some-template" type="text/x-handlebars-template">
   {{#users}}
     {username}
     {email}
</script>

I want to out-source it to a file called "user_template.js" which will look like this:

   {{#users}}
     {username}
     {email}

and make in the main index.html this link:

<script id="some-template" type="text/x-handlebars-template" src="user_template.js"></script>

The problem is - it doesn't work - how do I do it?

You can use ajax to load the template file.

With jQuery:

$.get("user_template.js", function(template_text){
    var template = Handlebars.compile(template_text);
    // more things
});