使用 Handlebars.js 时如何将模板 js 外包到不同的文件 [英] How to outsource a template js to a different file when using Handlebars.js

查看:22
本文介绍了使用 Handlebars.js 时如何将模板 js 外包到不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模板脚本

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

我想将其外包到一个名为user_template.js"的文件中,该文件将如下所示:

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

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

并在主 index.html 中创建此链接:

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?

推荐答案

可以使用ajax加载模板文件.

You can use ajax to load the template file.

使用 jQuery:

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

这篇关于使用 Handlebars.js 时如何将模板 js 外包到不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆