了解 Jquery 模板 [英] understanding Jquery template

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

问题描述

我正在阅读并试图理解一个 Jquery 模板示例.

I am reading and trying to understand a Jquery template example.

<script id="movieTemplate" type="text/x-jquery-tmpl"> 
  {{tmpl "titleTemplate"}}
  <tr class="detail"><td>Director: ${Director}</td></tr>

</script>

<table><tbody id="movieList"></tbody></table>

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

/* Convert the markup string into a named template,
   referenced by the {{tmpl}} tag */
$.template( "titleTemplate", "<tr class='title'><td>${Name}</td></tr>" );

/* Render the movies data, using the named template as a nested template */
$( "#movieTemplate" ).tmpl( movies ).appendTo( "#movieList" );
</script>

在这个示例程序中,我无法理解:

In this example program I am not able to understand about the:

/* 将标记字符串转换为命名模板,由 {{tmpl}} 标签引用 */

/* Convert the markup string into a named template, referenced by the {{tmpl}} tag */

当我们调用时:$( "#movieTemplate" ).tmpl( movies ) 它正在调用模板,我们正在调用带有输入电影的模板函数并将其附加到 movieslistid

when we call: $( "#movieTemplate" ).tmpl( movies ) it is calling the template on that we are calling the template function with input movies and appending that to movieslistid

如果我删除代码

$.template( "titleTemplate", "<tr class='title'><td>${Name}</td></tr>" );

它不起作用.你能解释一下为什么我们需要这个吗?它在这里做了什么,比如:/* 将标记字符串转换为命名模板,意思和所有......

it does not work. Can you please explain why we need this and what is it doing here like what does: /* Convert the markup string into a named template, mean and all..

我尝试在线阅读,发现我没有得到澄清

I tried to readonline and found that I am not getting this clarified

推荐答案

这包含对名为 "titleTemplate" 的模板的引用,该模板尚未定义:

This contains a reference to a template named "titleTemplate", a template which has not yet been defined:

<script id="movieTemplate" type="text/x-jquery-tmpl"> 
  {{tmpl "titleTemplate"}}
  <tr class="detail"><td>Director: ${Director}</td></tr>
</script>

这一行定义了缺少的模板:

This line defines that missing template:

$.template( "titleTemplate", "<tr class='title'><td>${Name}</td></tr>" );

这是另一种说法

<script id="titleTemplate" type="text/x-jquery-tmpl"> 
  <tr class='title'><td>${Name}</td></tr>
</script>

本质上,该示例表明您可以通过两种方式定义模板

In essence the example shows that you can define templates in two ways

  • 在 HTML 源代码中以声明方式,作为 <script type="text/x-jquery-tmpl"> 元素
  • 通过 $.template()
  • 以编程方式从字符串
  • declaratively in the HTML source code, as <script type="text/x-jquery-tmpl"> elements
  • programmatically from strings through $.template()

这篇关于了解 Jquery 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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