当前位置:Gxlcms > JavaScript > 介绍jquery.mustache.js的使用实例

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

时间:2021-07-01 10:21:17 帮助过:11人阅读

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);

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

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

人气教程排行