简体   繁体   English

从客户端隐藏HTML模板

[英]Hiding HTML template from client side

I want to keep my client-side code as clean and minimal as possible. 我想让我的客户端代码尽可能干净和最小。 My project uses Knockout JS and this template engine: https://github.com/blueimp/JavaScript-Templates . 我的项目使用Knockout JS和以下模板引擎: https : //github.com/blueimp/JavaScript-Templates I've created the template at the bottom of my source code with the computed values rendering in a "pre" section per the teamplate (that I don't really want visible). 我已经在源代码的底部创建了模板,并在每个团队标识的“ pre”部分中呈现了计算值(我确实不希望看到)。 Is there a way to hide the template and call it when the DOM loads? 有没有一种方法可以隐藏模板并在DOM加载时调用它?

To load your templates from the server, put them in their own little .html files and then on the client, you can use jquery to fetch them into a variable: 要从服务器加载模板,请将其放入自己的.html小文件中,然后在客户端上,可以使用jquery将其提取到变量中:

// The variable that we're going to put the template in
var myTemplate;

$.ajax({
      async: false,
      type: "GET",
      url: "/templates/myTemplate.html",
      success : function(data) {
            myTemplate = data;
      }
 });

Then when you want to use the template, you can call templ() with the template: 然后,当您要使用模板时,可以使用模板调用templ()

 var result = tmpl(myTemplate, data);

Note: I've forced jquery to be synchronous using async:false because you want to make sure the template is there when you call on it. 注意:我已强制使用async:false将jquery同步,因为您要确保在调用模板时该模板在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM