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

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

问题描述

我有这个模板脚本

 < script id =some-templatetype =text / x-handlebars -template> 
{{#users}}
{用户名}
{电子邮件}
< / script>

我想将它外联到一个名为user_template.js的文件,它看起来像这样:

  {{#users}} 
{用户名}
{email}

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

 < script id =some-templatetype =text / x-handlebars-templatesrc =user_template.js>< / script> 

问题是 - 不起作用 - 我该如何处理您可以使用ajax来加载模板文件。




$ b

解决方案

使用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
});

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

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