load jQuery-Templates from external file?

后端 未结 3 1180
滥情空心
滥情空心 2020-12-14 02:08

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

相关标签:
3条回答
  • 2020-12-14 02:16

    I recently wrote a javascript library to assist with this:

    https://www.github.com/stevenmhunt/tmpl.loader

    You can add jsRender or any other kind of template files using the <link> tag and they automatically register.

    0 讨论(0)
  • 2020-12-14 02:17

    you can load this template with ajax.

    <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.

    0 讨论(0)
  • 2020-12-14 02:22

    I wrote litle lib as jQuery plugin for ajax rendering html on appear block with indexDB caching in browser jQuery HTML Template Loader

    0 讨论(0)
提交回复
热议问题