首页 > web前端 > js教程 > 正文

介绍jquery.mustache.js的使用实例

零下一度
发布: 2017-06-17 17:53:35
原创
3112人浏览过

jquery.mustache是用jquery做了一层封装,提供了以下几个方法,让模板使用更便捷。
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);

返回一个字符串(渲染后的模板内容)

ViiTor实时翻译
ViiTor实时翻译

AI实时多语言翻译专家!强大的语音识别、AR翻译功能。

ViiTor实时翻译 116
查看详情 ViiTor实时翻译

(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>&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的使用实例的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号