使用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.应用场景: ...
随机推荐
- Linux-实用的工具
1.Nethogs-查看进程占用带宽情况wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # ...
- Oracle中的不等于号
今天碰到一个Oracle不等于的问题,最后搜索了一下,发现下面资料,拿来跟大家分享一下 关于Oracle中的不等于号: 在Oracle中, <> != ~= ^= 都是不等于号的意思. ...
- eclipse提交subversion+apache的中文路径问题解决过程
原文链接:http://cavenfeng.iteye.com/blog/1921400 .download & patch: # wget http://webdav.todo.gr.jp/ ...
- Blitz Templates介绍
Blitz Templates Blitz Templates-应用于大型互联网项目的非常强大非常快的模板引擎. 下载: sourceforge, 源代码 主页, win32 二进制文件, 其他语 ...
- http meta
定义和用法 <meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头部,不包含任 ...
- c#语音报时(含完整的声音资源文件).rar
private void btnBaoshi_Click(object sender, EventArgs e) { try { System.Threading.Thread thread = ne ...
- DB2 组内分组排序,游标使用
CREATE PROCEDURE Sys_Init_tblaccountsuser_sortid () P1: BEGIN '; ; ; ; DECLARE CUR1 CURSOR WITH RETU ...
- jQuery实现文字放大效果
实现效果:当鼠标移动到超链接的那一瞬间就出现提示. <!DOCTYPE html> <html> <head> <meta charset="UTF ...
- 剑指offer系列55---最小的k个数
[题目] 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. *[思路]排序,去除k后的数. package com.exe11 ...
- IE11登陆交行网银崩溃
1.打开IE11兼容性视图 2.交通银行就填入95559.com.cn 3.重新打开IE11登录网银