作者:zccst

jquery.mustache是用jQuery做了一层封装,提供了以下几个方法,让模板使用更便捷。

1,添加模板,使用异步的方式
var viewData = { name: 'Jonny' };
$.Mustache.load('./templates/greetings.htm')
.done(function () {
    $('body').mustache('simple-hello', viewData);
});
// 详见下面 ./templates/greetings.htm源码

2,添加模板的其他几种方式。add, addFromDom
//直接行内添加
$.Mustache.add('string-template', '<p>Hi, {{name}}, this is an inline template<p>');
// These two are identical(相同的), the latter just provides a terser(简洁的) syntax.
$.Mustache.add('dom-template', $('#dom-template').html());
$.Mustache.addFromDom('dom-template');//包括从外界读取
$('#target').mustache('string-template', viewData, { method: 'prepend' });

3,The mustache selector also allows you to pass an Array of View Models to render which makes populating lists a breeze:
// Clear #someList and then render all the viewModels using the list-template.
var viewModels = [
    { name: 'Jonny' },
    { name: 'nock' },
    { name: 'lucy' }
];//注意是数组。
$('#target').empty().mustache('string-template', viewModels);

4,支持partials
$.Mustache.load('./templates/templates.htm')
.done(function () {
    // Renders the `webcontent` template and the `footer-fragment` template to the page.
    $('body').mustache('webcontent', { year: 2012, adjective: 'EPIC' });
});
// 详见下面 ./templates/templates.htm源码

5,templates(), add(), clear()其他方法
console.log($.Mustache.templates());//查看已add的所有模板
console.log($.Mustache.has('string-template'));//查询某一个模板是否存在
$.Mustache.clear(); //清除所有已add的模板,变空了
console.log($.Mustache.templates());//经测试,已经空了

./templates/greetings.htm源码

<script id="simple-hello" type="text/html">
<p>Hello, {{name}}, how are you?</p>
</script>

./templates/templates.htm源码

<script id="footer-fragment" type="text/html">
<p>&copy; Jonny {{year}}</p>
</script>
<script id="webcontent" type="text/html">
<h1><blink>My {{adjective}} WebSite!</blink></h1> {{! Insert the `footer-fragment` template below }}
{{>footer-fragment}}
</script>

jquery.mustache.js使用的更多相关文章

  1. mustache.js使用基本(三)

    作者:zccst 本节要点是子模块(partials)和分隔符(delimiter)等 1,子模块(partials) /* {{>partials}}以>开始表示子模块,如{{> ...

  2. jquery和Js的区别和基础操作

    jqery的语法和js的语法一样,算是把js升级了一下,这两种语法可以一起使用,只不过是用jqery更加方便 一个页面想要使用jqery的话,先要引入一下jqery包,jqery包从网上下一个就可以, ...

  3. jQuery.template.js 简单使用

    之前看了一篇文章<我们为什么要尝试前后端分离>,深有同感,并有了下面的评论: 我最近也和前端同事在讨论这个问题,比如有时候前端写好页面给后端了,然后后端把这些页面拆分成很多的 views, ...

  4. 修改 jquery.validate.js 支持非form标签

    尝试使用markdown来写一篇blog,啦啦啦 源代码传送门:github 在特殊情况下我们使用jquery.validate.js对用户输入的内容做验证的时候,表单并不是一定包含在form之中,有 ...

  5. 表单验证插件之jquery.validate.js

    提到表单验证的插件,第一个想到的就是jquery.validate.js,所以小生想在这里稍微详细地说一下这款插件的具体使用方法,便于理解,我直接附上整段demo的代码(没怎么调样式,主要是看js): ...

  6. 延迟加载外部js文件,延迟加载图片(jquery.lazyload.js和echo,js)

    js里一说到延迟加载,大都离不开两种情形,即外部Js文件的延迟加载,以及网页图片的延迟加载: 1.首先简单说一下js文件的3种延迟加载方式: (1)<script type="text ...

  7. 初探jquery.slimscroll.js和iscroll5.js

    网上关于实现各种滚动效果的插件不胜枚举,这里,我简单介绍一下自己用过的两款比较有代表性的插件: 1.jquery.slimscroll.js,需要先引入jquery类库,主要用于模拟传统的浏览器滚动条 ...

  8. 动态生成二维码插件 jquery.qrcode.js

    前段时间做项目,需要动态生成一个二维码,于是就在网上找了一下发现一个jquery插件jquery.qrcode.js,所以今天就简单说一下这个插件的使用: jquery.qrcode.js是依赖jqu ...

  9. Jquery客户端校验——jquery.validate.js

    jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证 ...

随机推荐

  1. 【A + B + C + D】 问题

    A + B + C + D Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. checking for known struct flock definition... configure: error: Don't know how to define struct flock on this system, set --enable-opcache=

    在对php进行安装的过程中出现如下错误: 1.报错信息: 1 checking for known struct flock definition... configure: error: Don't ...

  3. linux命令chown和chmod什么区别

    chown一般用来 更改属主.也就是文件所属用户.chmod功能要比chown要强大.可更改文件所有属性和权限.只有管理员账户才有权限用此命令. chown 是修改文件的所有者(owner),和所属组 ...

  4. Java中常见的5种WEB服务器介绍

    这篇文章主要介绍了Java中常见的5种WEB服务器介绍,它们分别是Tomcat.Resin.JBoss.WebSphere.WebLogic,需要的朋友可以参考下 Web服务器是运行及发布Web应用的 ...

  5. IE6下绝对定位元素和浮动元素并列绝对定位元素消失

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. linker command failed with exit code 1 (use -v to see invocation)修改方法

    它的意思是指文件丢失或者文件重复,你就要仔细查看项目中有没有重复的文件,如有重复修改即可,如果是文件丢失,那就麻烦啦!你需要根据报错的地方进行修补文件,最为直接的方法是重新创建项目.还有另外一种情况是 ...

  7. Swift学习(1)

    swif(1) println("Hello, world") 输出结果: Hello, world swift使用let来声明常量,使用var来声明变量 //变量 var myV ...

  8. HDU 3552 I can do it!

    脑洞题.http://blog.csdn.net/dgq8211/article/details/7748078 #include<cstdio> #include<cstring& ...

  9. Day05_JAVAEE系列:Junit

    Junit 单元测试开发工具:myeclipse10 内部集成的junit的库 1)什么是junit?     junit是一个专业的编写测试代码的工具 2)建立junit测试类的步骤:     2. ...

  10. JSON缺包导致的错误

    Json-lib 需要的 jar 包 commons-beanutils-1.8.3.jar commons-collections-3.2.1.jar commons-lang-2.6.jar co ...