介绍jquery.mustache.js的使用实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了介绍jquery.mustache.js的使用实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3680字,纯文字阅读大概需要6分钟。
内容图文
![介绍jquery.mustache.js的使用实例](/upload/InfoBanner/zyjiaocheng/301/ec83d322c56d47a88c0663342f6033fe.jpg)
1,添加模板的三种方式
add,
addFromDom
addFromString
可以直接添加模板,无论是作为字符串文字或引用其他的DOM元素
(1)template 是字符串直接量
//add仅仅是把template添加到当前页面,注意并没有渲染 $.Mustache.add('string-template', '<p>Hi, {{name}}, this is an inline template<p>');
(2)引用DOM元素 addFromDom
// 两者是相同的,后者有更简洁的语法 These two are identical, the latter just provides a terser syntax. $.Mustache.add('dom-template', $('#dom-template').html()); $.Mustache.addFromDom('dom-template');
如果你更愿意将模板存储在DOM中(假设从外部文件载入它们),此时你可以仅调用
$.Mustache.addFromDom(),不用任何参数,这样的话将读取你模板中所有<script type=”text/html” />块。
(3)载入外部模板(html文件),然后渲染
a, 不依赖模块化的情况,直接使用自带的 $.Mustache.load() 方法
var viewData = { name: 'Jonny' }; $.Mustache.load('./templates/greetings.htm').done(function () { $('body').mustache('simple-hello', viewData); });
在上面的例子中,我们载入了外部模板(html文件),一旦载入成功,就渲染他里面的元素。
外部模板应该定义在script标签块中,script标签快的id将用来作为模板名称,本例中是simple-hello
// 详见下面
./templates/greetings.htm源码
<script id="simple-hello" type="text/html"> <p>Hello, {{name}}, how are you?</p> </script>
b, 依赖模块化的情况,先使用require载入文件,再使用mustache读取文件内容(addFromString)
//1,准备工作 require('jQueryMustache'); var tpl = require("crownSheetTpl"); $.Mustache.addFromString(tpl); //2,使用 this.$el.empty().mustache('crownSheet-' + templateChoice + '-Template',this.model);
2,渲染的两种方式
(1)全局的 $.Mustache.render() 方法
$.Mustache.render(‘my-template’, viewData);
返回一个字符串(渲染后的模板内容)
(2)jQuery的mustache选择器
$(“#someElement”).mustache(‘my-template’, viewData);
返回一个jQuery选择器链接。
这种方式默认的是将渲染后的模板内容追加到DOM选择器元素中,但是你仍然可以改变这种行为,通过传递一个可选的method参数。
// Replace the contents of #someElement with the rendered template.
$(‘#someElement’).mustache(‘my-template’, viewData, { method: ‘html’ });
// Prepend the rendered Mustache template to #someElement.
$(‘#someElement’).mustache(‘my-template’, viewData, { method: ‘prepend’ });
mustache选择器也允许你传一个模型数组,这使得你渲染数组变成轻而易举的事
(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' } ];//注意是数组。 $('#someList').empty().mustache('list-template', viewModels);
首先清除someList的内容,然后用数组viewModels渲染到列表模板list-template中。
3,根据调试等需要的其他方法,如templates(), add(), clear()
为了便于调试,你可以使用$.Mustache.templates()方法获取所有注册的模板。 //查看已add的所有模板 console.log($.Mustache.templates()); //查询某一个模板是否存在 console.log($.Mustache.has('string-template')); //你可以调用$.Mustache.clear清除所有模板 $.Mustache.clear(); //清除所有已add的模板,变空了 //经测试,已经空了 console.log($.Mustache.templates());
4,最后,支持部分渲染 partials,只需要保证被提前载入
$.Mustache.load('./templates/templates.htm').done(function () { // 渲染`webcontent`模板 和 `footer-fragment`子模板. $('body').mustache('webcontent', { year: 2012, adjective: 'EPIC' }); });
// 详见下面
./templates/templates.htm源码
<script id="footer-fragment" type="text/html"> <p>© 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的使用实例的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的介绍jquery.mustache.js的使用实例全部内容,希望文章能够帮你解决介绍jquery.mustache.js的使用实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。