使用mustache.js 模板引擎输出html
看了https://mustache.github.io/你就知道mustache是非常强大的模板引擎,支持多种语言,下面是个简单入门例子:
MVC Model
public class StudentModel
{
[Key]
public int StuId { get; set; } [Display(Name = "姓名")]
[StringLength()]
[Required(ErrorMessage = "姓名必填")]
public string StuName { get; set; }
[StringLength()]
[Display(Name = "住址")]
[DataType(DataType.MultilineText)]
//[Required(ErrorMessage = "住址必填")]
public string Address { get; set; } [Display(Name = "年龄")]
[Required(ErrorMessage = "年龄必填")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public int Age { get; set; }
}
StudentModel
Mvc5WebContext
public class Mvc5WebContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx public Mvc5WebContext() : base("name=Mvc5WebContext")
{
} public System.Data.Entity.DbSet<Mvc5Web.Models.StudentModel> StudentModels { get; set; } }
Mvc5WebContext
Controller
public class StudentController : Controller
{
public JsonResult JsonList()
{
//StudentModel stu = new StudentModel { StuId = 100, StuName = "wilson100", Age = 100, Address = "HG" };
return Json(db.StudentModels, JsonRequestBehavior.AllowGet);
}
}
html模板
<script id="template" type="text/html">
<table border="">
<tr>
<th>姓名</th>
<th>地址</th>
<th>年龄</th>
</tr>
{{#studentLists}}
<tr>
<td>{{StuName}}
</td>
<td>{{Address}}
</td>
<td>{{Age}}
</td>
</tr>
{{/studentLists}}
</table>
</script>
Ajax请求进行客户端模板输出
$("#btnTest").click(function () {
$.get("JsonList", function (data) {
var template = $('#template').html();
Mustache.parse(template); // optional, speeds up future uses
var rendered = Mustache.render(template, { studentLists: data });
$('#target').html(rendered);
});
});
mustache.js官方信息
https://github.com/janl/mustache.js
使用mustache.js 模板引擎输出html的更多相关文章
- vue系列---Mustache.js模板引擎介绍及源码解析(十)
mustache.js(3.0.0版本) 是一个javascript前端模板引擎.官方文档(https://github.com/janl/mustache.js) 根据官方介绍:Mustache可以 ...
- 调研js模板引擎
js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...
- js模板引擎介绍搜集
js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...
- js模板引擎--artTemplate
js模板引擎--artTemplate 以前研究过一段时间的handlebars,但因为其渲染性能略逊于腾讯的artTemplate(在artTemplate的GitHub官网上有推荐的性能测试地址) ...
- 浅析js模板引擎
js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...
- doT js 模板引擎【初探】要优雅不要污
js中拼接html,总是感觉不够优雅,本着要优雅不要污,决定尝试js模板引擎. JavaScript 模板引擎 JavaScript 模板引擎作为数据与界面分离工作中最重要一环,越来越受开发者关注. ...
- 各种JS模板引擎对比数据(高性能JavaScript模板引擎)
最近做了JS模板引擎测试,拿各个JS模板引擎在不同浏览器上去运行同一程序,下面是模板引擎测试数据:通过测试artTemplate.juicer与doT引擎模板整体性能要有绝对优势: js模板引擎 Ja ...
- Filter - Surge.js模板引擎过滤器
版权所有,转载请注明出处:http://guangboo.org/2014/01/05/filter-surgejs-template-engine 过滤器在surge.js模板引擎中多处用到,其类似 ...
- 百度JS模板引擎 baiduTemplate 1.0.6 版
A.baiduTemplate 简介 0.baiduTemplate希望创造一个用户觉得“简单好用”的JS模板引擎 注:等不及可以直接点左侧导航中的”C.使用举例“,demo即刻试用. 1.应用场景: ...
随机推荐
- opsview
nagios,cacti,opsview,prtg,zabbix http://www.opsview.com 1.需要注册一个账号,createyuan#sohu.com
- 一个项目中说系统分为表现层、控制层、逻辑层、DAO层和最终数据库五层架构-转
表现层就是看到的东西,比如你现在看到的当前页面控制层就将你的请求从页面传到后台代码逻辑层就是处理你的请求的代码DAO层就是将数据存到数据库中的代码数据库就是数据库了,存东西用的 ,DAO层就是将访问数 ...
- 注解:@Autowired
Spring的bean对象自动装配注解,@Autowired有三种使用方式,下面分别介绍! 1. 作用于类成员变量上,即在定义类的成员变量的时候,至于其上方. public class Mobile{ ...
- jquery 事件绑定以及解绑定
var targetSelect = $("#@(Perfix)tbData tbody tr select[data-target]"); targetSelect.off(&q ...
- 多线程编程之Linux环境下的多线程(二)
上一篇文章中主要讲解了Linux环境下多线程的基本概念和特性,本文将说明Linux环境下多线程的同步方式. 在<UNIX环境高级编程>第二版的“第11章 线程”中,提到了类UNIX系统中的 ...
- IOS-归档的使用
归档又叫序列化(coding) // 归档NSKeyedArchiver // 解归档NSKeyedUnarchiver // 归档之后的文件 会被加密 // 可以归档的文件.数据--> ...
- javascript url 相关函数(escape/encodeURL/encodeURIComponent区别)
JS获取url参数及url编码.解码 完整的URL由这几个部分构成:scheme://host:port/path?query#fragment ,各部分的取法如下: window.location. ...
- Jedis编程设计:连接池
Jedis作为redis的最佳客户端,它提供了连接池的特性,"连接池"在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式.Jedis的连接池设计基于apa ...
- Css3_写出小广播样子
/* create an arrow that points up */ div.arrow-up { width:0px; height:0px; border-left:5px solid tra ...
- 黄聪:发送键盘指令System.Windows.Forms.SendKeys.Send
若要指定在按键(如 Enter 或 Tab)时不显示的字符,以及表示操作而不表示字符的键,请使用下表中的代码: 键 代码 Backspace {BACKSPACE}.{BS} 或 {BKSP} B ...