【问题标题】:understanding Jquery template了解 Jquery 模板
【发布时间】:2012-11-21 06:34:12
【问题描述】:

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

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

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

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

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

如果我删除代码

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

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

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

【问题讨论】:

    标签: jquery jquery-plugins jquery-templates


    【解决方案1】:

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

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

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

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

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

    • 在 HTML 源代码中以声明方式,如 &lt;script type="text/x-jquery-tmpl"&gt; 元素
    • 以编程方式从字符串到$.template()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      相关资源
      最近更新 更多