看了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官方信息

http://mustache.github.io/

https://github.com/janl/mustache.js

使用mustache.js 模板引擎输出html的更多相关文章

  1. vue系列---Mustache.js模板引擎介绍及源码解析(十)

    mustache.js(3.0.0版本) 是一个javascript前端模板引擎.官方文档(https://github.com/janl/mustache.js) 根据官方介绍:Mustache可以 ...

  2. 调研js模板引擎

    js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...

  3. js模板引擎介绍搜集

    js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...

  4. js模板引擎--artTemplate

    js模板引擎--artTemplate 以前研究过一段时间的handlebars,但因为其渲染性能略逊于腾讯的artTemplate(在artTemplate的GitHub官网上有推荐的性能测试地址) ...

  5. 浅析js模板引擎

    js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...

  6. doT js 模板引擎【初探】要优雅不要污

    js中拼接html,总是感觉不够优雅,本着要优雅不要污,决定尝试js模板引擎. JavaScript 模板引擎 JavaScript 模板引擎作为数据与界面分离工作中最重要一环,越来越受开发者关注. ...

  7. 各种JS模板引擎对比数据(高性能JavaScript模板引擎)

    最近做了JS模板引擎测试,拿各个JS模板引擎在不同浏览器上去运行同一程序,下面是模板引擎测试数据:通过测试artTemplate.juicer与doT引擎模板整体性能要有绝对优势: js模板引擎 Ja ...

  8. Filter - Surge.js模板引擎过滤器

    版权所有,转载请注明出处:http://guangboo.org/2014/01/05/filter-surgejs-template-engine 过滤器在surge.js模板引擎中多处用到,其类似 ...

  9. 百度JS模板引擎 baiduTemplate 1.0.6 版

    A.baiduTemplate 简介 0.baiduTemplate希望创造一个用户觉得“简单好用”的JS模板引擎 注:等不及可以直接点左侧导航中的”C.使用举例“,demo即刻试用. 1.应用场景: ...

随机推荐

  1. sed命令拷屏

    http://blog.sina.com.cn/s/blog_45497dfa0100w6r3.html  sed样例较多,可以参考 http://blog.sina.com.cn/s/blog_6d ...

  2. WebApiThrottle限流框架

    ASP.NET Web API Throttling handler is designed to control the rate of requests that clients can make ...

  3. ArrayList和LinkedList遍历方式及性能对比分析

    ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayLis ...

  4. XAMPP 在windows下无法启动Apache解决方案

    XAMPP 在windows下无法启动Apache解决方案 一.现象 XAMPP 点击Start Apache时出现如下错误 20:41:12  [Apache] Error: Apache shut ...

  5. angularjs webstorm 单元测试 Package.json

    Package.json小结 生成package.json 定位到想放置package.json的目录,运行npm init,根据提示就可以生成package.json文件,其中test comman ...

  6. SqlHelper文件复习

    SqlHelper这是个cs类文件,将经常使用到的数据库操作写到一个文件下,方便调用,以及减少代码量.使用这个文件的前提是要建立一个app.config文件,并且引用configuration程序集: ...

  7. 【freemaker】之整合springMVC

    pom.xml文件 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncod ...

  8. [HTMLDOM]删除已有的 HTML 元素

    摘自www.w3school.com:http://www.w3school.com.cn/htmldom/dom_elements.asp如需删除 HTML 元素,您必须清楚该元素的父元素: < ...

  9. Report_客制化报表输出Excel后去0问题(案例)

    2014-02-01 Created By BaoXinjian

  10. java学习日志(1):命令行and小程序

    1.dos命令行,常见的命令 dir:列出当前目录下的文件以及文件夹md:创建目录rd:删除目录(必须空)cd:进入指定目录cd.. :退回到上一级目录cd/:退回到根目录del:删除文件exit:退 ...