最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 20:19:40
文档

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

介绍jquery.mustache.js的使用实例:jquery.mustache是用jQuery做了一层封装,提供了以下几个方法,让模板使用更便捷。1,添加模板的三种方式add,addFromDomaddFromString可以直接添加模板,无论是作为字符串文字或引用其他的DOM元素(1)template 是字符串直接量//add仅仅是把t
推荐度:
导读介绍jquery.mustache.js的使用实例:jquery.mustache是用jQuery做了一层封装,提供了以下几个方法,让模板使用更便捷。1,添加模板的三种方式add,addFromDomaddFromString可以直接添加模板,无论是作为字符串文字或引用其他的DOM元素(1)template 是字符串直接量//add仅仅是把t


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>© 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的使用实例

介绍jquery.mustache.js的使用实例:jquery.mustache是用jQuery做了一层封装,提供了以下几个方法,让模板使用更便捷。1,添加模板的三种方式add,addFromDomaddFromString可以直接添加模板,无论是作为字符串文字或引用其他的DOM元素(1)template 是字符串直接量//add仅仅是把t
推荐度:
标签: 使用 介绍 js
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top