作者: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. 转载,find.sh

    #!/bin/bash #find files contains a keyword #write by xiaojing.zhao #2012.12.14 echo -e "\nThis ...

  2. hover带有动画效果的导航

    html,body{overflow-x:hidden;} ul,li{list-style: none;} .nav{width:100%; height: 26px; overflow: hidd ...

  3. HTML之组件margin、padding

    1.  HTML之组件可以通过CSS里的width height进行大小控制 2.HTML之组件可以通过CSS里的margin.padding进行组件和组件间的间距 margin/padding:(u ...

  4. json的学习笔记

    json比较简单,所以先从json开始学起. 一 json的名称: json的全称是javascript object notation,中文名称为js 对象表示法. json的定义:json是一种轻 ...

  5. php 判断是不是https链接

    如果当前运行的 PHP 版本等于或高于提供的版本号,该函数返回布尔值 TRUE ,反之则返回 FALSE . function is_https() { if (!empty($_SERVER['HT ...

  6. curl file_get_contents fsockopen

    三种处理的方式:     curl     file_get_contents     fsockopen fsockopen 是比较底层的调用,属于网络系统的socket调用,而curl经过的包装支 ...

  7. linux 下载安装tomcat

    1.下载tomcat 2.tar -zxvf apache-tomcat-7.0.67.tar.gz 3.cd bin目录  启动  ./startup.sh [root@fuyuanming bin ...

  8. useradd adduer 的区别

    区别 1). 使用useradd时,如果后面不添加任何参数选项,例如:#sudo useradd test创建出来的用户将是默认“三无”用户:一无Home Directory,二无密码,三无系统She ...

  9. elasticsearch使用jetty进行简单的权限控制

    默认elasticsearch是使用netty作为http的容器,由于netty并没有权限模块,所以默认es没有任何的权限控制,直接通过http就可以进行任何操作,除非把http禁用.但如果你使用el ...

  10. DedeCMS中实现在顶层banner中显示自定义登录信息

    一.需求描述 dedeCMS自带的模板中有互动中心模块,如下图所示: 由于会员登陆对我来说不是网站的重要模块且默认DedeCMS的会员中心模块的初始化很慢,常会显示“正在载入中,请稍候...”, 所以 ...