从外部文件加载jQuery模板? [英] load jQuery-Templates from external file?

查看:81
本文介绍了从外部文件加载jQuery模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用jQuery的模板引擎。到目前为止看起来还不错。但是我想知道是否可以通过某种方式从外部文件加载模板。想象一下有大量的模板。这会弄乱html代码,并且也无法缓存,因此必须在每次请求时都进行下载。

I just started using jQuery's template engine. Which looks quite nice so far. Yet i wonder if it's possible to load templates from an external file somehow. Imagine having loads of templates. This would mess up the html-code and is also not cacheable and has to be downloaded on every request.

我希望有一种方法可以在外部文件中全部定义它们,然后加载它们并将已编译的模板存储到localStorage中。

I was hoping there is a way to define them all in an external file and then load them and store the compiled templates into localStorage.

有人知道如何从外部文件加载它们吗?

Does anyone have an idea how to load them from an external file?

推荐答案

您可以加载此文件

<script>
  var movies = [
    { Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" },
    { Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
    { Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
  ];

  $.get("templates/movieTemplate.html", function(data, textStatus, XMLHttpRequest){
    var markup = data; //"<tr><td colspan='2'>${Name}</td><td>Released: ${ReleaseYear}</td><td>Director: ${Director}</td></tr>"

    /* Compile markup string as a named template */
    $.template( "movieTemplate", markup );

    /* Render the named template */
    $.tmpl( "movieTemplate", movies ).appendTo( "#movieList" );
  });
</script>

现在,如果要加载任何模板,则可以为已加载的模板添加本地存储逻辑或数组只有一次。

You can add now the localstorage logic or an array for the loaded templates if you want to load any template only once.

这篇关于从外部文件加载jQuery模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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