jQuery.Template虽然用起来没有Mustache简洁和方便,还是学习了解一下,做个笔记。

模板可以定义在页面script标签,如下

  1. <script type="text/html" id="template">
  2. <div data-content="author"></div>
  3. <div data-content="date"></div>
  4. <img data-src="authorPicture" data-alt="author"/>
  5. <div data-content="post"></div>
  6. </script>

也可以定义到独立的html文件中,好处是可以使用浏览器缓存,例如:

  1. <div style="margin:50;border:solid 1px red">
  2.  
  3. <div data-content-text="author"></div>
  4. <div data-content="date"></div>
  5. <img data-src="authorPicture" data-alt="author" />
  6. <div data-content="post"></div>
  7. </div>
  8.  
  9. <hr />

在客户端调用,如果数组数据,模板自动循环重复输出

  1. //$("#template-container").loadTemplate($("#template"),
  2. // {
  3. // author: 'Joe Bloggs',
  4. // date: '25th May 2013',
  5. // authorPicture: 'https://www.baidu.com/img/bd_logo1.png',
  6. // post: 'This is the contents of my post'
  7. // });
  8.  
  9. $("#template-container").loadTemplate("Templates/template.html",
  10. [{
  11. author: 'Joe Bloggs',
  12. date: '25th May 2013',
  13. authorPicture: 'https://www.baidu.com/img/bd_logo1.png',
  14. post: 'This is the contents of my post'
  15. },
  16. {
  17. author: 'Wilson就是看到看看',
  18. date: '25th May 2013',
  19. authorPicture: 'https://www.baidu.com/img/bd_logo1.png',
  20. post: 'This is the contents of my post'
  21. }
  22. ]);

使用jquery.template 输出table

html:

  1. <table border="1">
  2. <thead>
  3. <tr>
  4. <th>Id</th>
  5. <th>Name</th>
  6. </tr>
  7. </thead>
  8. <tbody id="tbody">
  9. </tbody>
  10. </table>

template:

  1. <tr>
  2. <td data-content="id"></td>
  3. <td data-content="name"></td>
  4. </tr>

javascript:

  1. var data = [];
  2.  
  3. for (var i = 0; i < 10; i++) {
  4. data.push({ "id": i, "name": "user_name_" + i.toString() });
  5. }
  6. $("#tbody").loadTemplate("Templates/tbList.html", data);

参考:https://github.com/codepb/jquery-template

数据绑定属性

  1. There are a number of different bindings and ways to bind the data. The following attributes are available:
  2.  
  3. "data-innerHTML" (>= 1.4.5) - binds the value supplied to the content (innerHTML) of the element (uses $(elem).html(value))
  4. "data-content" - alias for the newer "data-innerHTML"
  5. "data-content-text" - binds the value supplied to the content of the element as text (uses $(elem).text(value))
  6. "data-content-append" - appends the value to the end of the element (uses $(elem).append(value))
  7. "data-content-prepend" - prepends the value to the beginning of the element (uses $(elem).prepend(value))
  8. "data-id" - sets the id of the element to the value provided (uses $(elem).attr("id", value));
  9. "data-href" - sets the href value of the element to the value provided (uses $(elem).attr("href", value));
  10. "data-alt" - sets the alt value of the element to the value provided (uses $(elem).attr("alt", value));
  11. "data-value" - sets the value attribute of the element to the value provided (uses $(elem).val(value))
  12. "data-class" - sets the class attribute of the element to the value provided (uses $(elem).class(value))
  13. "data-link" - sets the innerHtml of the element to be a link to the value provided (wraps the content in an <a> tag).
  14. "data-link-wrap" - wraps the element in a link to the value provided. Same as "data-link", but the <a> tag wraps the element as well as the content.
  15. "data-options" - adds options to a select box. The value for this should reference an array of strings, each option will be output as a separate option. The value will be the same as the displayed text for each option. For a more powerful version of this look at the data-template-bind option.

自定义格式化方法

  1. $.addTemplateFormatter({
  2. UpperCaseFormatter : function(value, template) {
  3. return value.toUpperCase();
  4. },
  5. LowerCaseFormatter : function(value, template) {
  6. return value.toLowerCase();
  7. },
  8. SameCaseFormatter : function(value, template) {
  9. if(template == "upper") {
  10. return value.toUpperCase();
  11. } else {
  12. return value.toLowerCase();
  13. }
  14. }
  15. });

模板

  1. <tr>
  2. <td data-content="id"></td>
  3. <td data-content="name" data-format="LowerCaseFormatter"></td>
  4. </tr>

jQuery.loadTemplate客户端模板的更多相关文章

  1. MVC下的客户端模板技术

    1.引言 在Web编程中,我们有时经常需要使用Ajax来访问服务端的接口,然后使用这些返回的数据(一般格式都是JSON)来展示客户端的相关信息.例如:在一个商品列表,我们点击某一样的商品,查看该商品的 ...

  2. javascript&&jquery编写插件模板

    javascrpt插件编写模板 这里不分享如何编写插件,只留一个框架模板,使用面向对象的形式进行编写,方便管理 ;(function(window,document){ function FnName ...

  3. jquery jtemplates.js模板渲染引擎的详细用法第三篇

    jquery jtemplates.js模板渲染引擎的详细用法第三篇 <span style="font-family:Microsoft YaHei;font-size:14px;& ...

  4. jquery jtemplates.js模板渲染引擎的详细用法第二篇

    jquery jtemplates.js模板渲染引擎的详细用法第二篇 关于jtemplates.js的用法在第一篇中已经讲过了,这里就直接上代码,不同之处是绑定模板的方式,这里讲模板的数据专门写一个t ...

  5. jquery jtemplates.js模板渲染引擎的详细用法第一篇

    jquery jtemplates.js模板渲染引擎的详细用法第一篇 Author:ching Date:2016-06-29 jTemplates是一个基于JQuery的模板引擎插件,功能强大,有了 ...

  6. jquery.tmpl.js 模板引擎用法

    1.0 引入: <script src="/js/jquery.tmpl.min.js"></script> 2.0 模板: <script type ...

  7. AMQ学习笔记 - 05. 客户端模板化

    概述 客户端编程模型中,大部分的步骤都是相同的.将相同的部分做成模板,将不同的部分预留接口,实现者就只需要针对不同的部分提供实现. 设计 类图 发送方客户端 说明: 基于模板的思想,SendTempl ...

  8. jquery判断客户端的类型

    针对不同客户端下载链接的页面响应样式不一样,更人性点而已 //匹配客户端类型 var isAndroid = navigator.userAgent.toLowerCase().match(/andr ...

  9. jquery的一个模板引擎-zt

    jQuery-jTemplate.js下载:http://jtemplates.tpython.com/ 一 , 简单介绍 它是一个基于jQuery开发的javascript模板引擎.它主要的作用如下 ...

随机推荐

  1. bing统计【转自CSDN博客】

    文章来源:http://blog.csdn.net/aa512690069/article/details/17918799 其原文是微软一个小题目:http://hero.csdn.net/Ques ...

  2. linux -samba

    yum install samba samba-client samba-swat samba-common-3.6.9-151.el6.x86_64 //主要提供samba服务器的设置文件与设置文件 ...

  3. mvn 命令

    mvn eclipse:eclipse  用mvn把文件转化成eclipse支持的文件 mvn install   打包jar,放到本地仓库 ,在打包的时候还会执行单元测试,不需要的话运行(不需要打入 ...

  4. paypal接口对接注意事项

    追加:新的设定画面 在paypal对接过程中,会存在return_url和notify两种 分别用pdt和ipn实现 但是对于paypal,大家请注意,真实环境和沙盒测试环境的区别 你可以到www.p ...

  5. TP框架基础

    什么是TP框架: 一堆代码的集合,里边有变量.函数.类.常量,设计模式MVC.AR数据库.单例等等.全称是Tinkphp框架; 为什么使用框架: 使用框架将全部精力集中在业务层次,节省50-60%的工 ...

  6. MongoDB shell 格式化

    直接的方法: db.collection.find().pretty(); 如果想要所有的查询都格式化,可以执行: echo "DBQuery.prototype._prettyShell ...

  7. Jquery实现购物车物品数量的加减特效

    今天网友翠儿在用Jquery实现购物车物品数量的加减特效的时候遇到问题来问我,我后来帮她解决了这个Jquery特效,现在把它整理出来分享给大家用,虽然功能比较简单,但是很实用. 主要包括了以下功能: ...

  8. 【freemaker】之循环,判断,对象取值

    entity: public class Employee { private Integer id; private String name; private Integer age; privat ...

  9. c++封装编写线程池

    在csapp学习或者其他linux底层编程的过程中,一般都会举一些多线程或多进程的例子,配合底层同步原语.系统调用api来解释怎么创建多线程/多进程. 但是这些例子和实际项目中所用到的多线程/多进程编 ...

  10. 使用Spring的命名空间p装配属性-摘自《Spring实战(第3版)》

    使用<property>元素为Bean的属性装配值和引用并不太复杂.尽管如此,Spring的命名空间p提供了另一种Bean属性的装配方式,该方式不需要配置如此多的尖括号. 命名空间p的sc ...