
最代码官方 LV168
2014年8月13日
你这牛币明显是要给我的咯。
这是mustache的模板语法,是属于前端的js模板引擎框架,注意不是后端java层的。
参考下这个前后端整合代码吧:bootstrap+jquery+mustache+springmvc+idworker整合demo代码
另外你截图提到的是用户发表评论后,ajax请求评论接口并返回评论对象,最后通过jquery得到mustache的模板再渲染后得到最终结果append到评论列表的尾部。
相关代码如下:
ajax提交评论js代码片断
01 | $( '.post_comment' ).click( function () { |
02 | var post_comment=$( this ); |
03 | var id=$( this ).attr( "id" ); |
04 | var ids=id.split( "__" ); |
05 | var commentsId=ids[0]; |
06 | var type=ids[1]; |
07 | var source_id=ids[2]; |
08 | var editor=eval( "CKEDITOR.instances.editor_" +type+ "_" +source_id); |
09 | if (editor.getData()== "" ){ |
10 | alert( "请输入评论内容" ); |
11 | return ; |
12 | } |
13 | var template = $( '#comment_template' ).html(); |
14 | if ($( this ).hasClass( 'disabled' )){ |
15 | return ; |
16 | } |
17 | post_comment.find( "a" ).text( "提交中..." ).addClass( 'disabled' ); |
18 | $.ajax({ |
19 | url : "/comment/create.htm" , |
20 | type : 'POST' , |
21 | dataType : 'json' , |
22 | data : { |
23 | content : editor.getData(), |
24 | type:type, |
25 | source_id:source_id |
26 | }, |
27 | success : function (json) { |
28 | if (json.error == "" ) { |
29 | var html = Mustache.to_html(template, json.comment).replace(/^\s*/mg, '' ); |
30 | $( "#" +commentsId+ " .datas" ).append(html); |
31 | editor.setData( "" ); |
32 | post_comment.find( "a" ).text( "评论" ).removeClass( 'disabled' ); |
33 | } else { |
34 | alert(json.error); |
35 | } |
36 | } |
37 | }); |
38 | }); |
mustache评论模板js script代码片断
01 | <script id= "comment_template" class= "template" type= 'text/template' > |
02 | <div class= "data row-fluid" > |
03 | <div class= "span1" > |
04 | {{ #user}} |
05 | <a class= "name" href= "/user/{{id}}.htm" rel= "nofollow" > |
06 | <img src= "{{avatarUrl}}" alt= "{{name}}的gravatar头像" /> |
07 | </a> |
08 | {{/user}} |
09 | </div> |
10 | <div class= "span11" > |
11 | <div class= "data_title" > |
12 | {{ #user}}<span><a class="name" href="/user/{{id}}.htm" rel="nofollow">{{name}}</a></span>{{/user}} |
13 | <span class= "time pull-right" >{{createTimeExt}}</span> |
14 | </div> |
15 | <div id= "content_{{id}}" class= "content clear" > |
16 | {{{content}}} |
17 | </div> |
18 | </div> |
19 | </div> |
20 | </script> |
注意:mustache的模板片断也可以用非script来替换,只要用id标示,jquery可以获取得到即可。但是用script有个优势就是搜索引擎会忽略此段代码,如果模板中正好有锚点,比如:
1 | <span style= "background-color:#FFFF00" ><a class= "name" href= "/user/{{id}}.htm" rel= "nofollow" > |
2 | <img src= "{{avatarUrl}}" alt= "{{name}}的gravatar头像" /> |
3 | </a></span> |
这样对seo是很不利的。
官方demo地址:http://mustache.github.io/#demo
其实诸如前端js模板引擎技术有好多
如:
template
官方参考:http://aui.github.io/artTemplate
BaiduTemplate
官方参考:http://baidufe.github.io/BaiduTemplate
juicer
官方参考:http://juicer.name
doT
官方参考:http://olado.github.io/doT
kissy
官方参考:http://docs.kissyui.com和https://github.com/kissyteam/kissy
最后选用mustache还是觉得简单而且确实强大。
评论(4)
最佳答案
- 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?

- 等 js这段脚本语法如何解释?
- 完 js如何实现二维数组各个对应位置相加的算法?
- 等 如何实现js弹出一个添加或修改的子窗口,然后提交刷新页面功能?
- 等 js如何将数组实现排列组合?
- 等 js中数字类型的字符串为什么不能转换为Number?
- 完 前端js点击每一行的删除图标删除行后保存怎么获取没有被隐藏的行?
- 等 js如何实现在一个图片上鼠标移动过去图片周围有光圈,在点击图片的时候有声音发出的特效?
- 完 js如何在文本框中输入5并且在下面出来五行呢?
- 等 js代码如何实现隐藏chrome浏览器的地址栏?
- 等 js怎么写提交表单的方法?
- 完 js怎么实现提交按钮后还是在当前页面?
- 完 如何理解js的这个语法?

longzitian LV3
2016年4月21日
老唱片之桥_ LV2
2015年11月3日
vip_zuidaima LV7
2015年10月31日
mjtljx LV42
2014年12月3日
linapex LV14
2014年10月14日
itachi LV11
2014年9月3日
George LV26
2014年9月3日
lietian LV8
2014年9月2日
hhybtcz LV2
2014年9月2日
s8855516 LV1
2014年8月27日