JavaScript templating engine(一个好的JS模板引擎)
2013-07-08 21:57
746 查看
地址:http://blueimp.github.io/JavaScript-Templates/
使用方式:
1、在客户端将模板文件引入
2、在客户端添加定义模板类型(o代表传入的json格式的数据)
3、在客户端定义按钮(用来向服务端发送Ajax请求,获取JSON格式的数据)和数据显示区域
4、按钮单击事件(用jquery处理)
5、在服务端添加Handler
6、定义Customer类
使用方式:
1、在客户端将模板文件引入
<script src="JS/tmpl.min.js"></script>
2、在客户端添加定义模板类型(o代表传入的json格式的数据)
<script type="text/x-tmpl" id="tmpl-demo"> <h3 style="color:red;">{%=o.Name%}</h3> <h4>年龄:{%=o.Age%}</h4> </script>
3、在客户端定义按钮(用来向服务端发送Ajax请求,获取JSON格式的数据)和数据显示区域
<div> <input type="button" value="点击" id="theBtn" /> </div> <div id="result"> </div>
4、按钮单击事件(用jquery处理)
<script type="text/javascript"> $(function () { $("#theBtn").click(function () { $.ajax({ type: "POST", url: "Handler/temp.ashx", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { console.log(msg); $("#result").html(tmpl("tmpl-demo", msg)); }, error: function () { alert("An unexpected error has occurred during processing."); } }); }); }); </script>
5、在服务端添加Handler
public void ProcessRequest (HttpContext context) { context.Response.AddHeader("Vary", "Accept"); try { if (context.Request["HTTP_ACCEPT"].Contains("application/json")) context.Response.ContentType = "application/json"; else context.Response.ContentType = "text/plain"; } catch { context.Response.ContentType = "text/plain"; } JavaScriptSerializer js = new JavaScriptSerializer(); js.MaxJsonLength = 41943040; Customer theCustomer = new Customer(); theCustomer.Name = "息壤"; theCustomer.Age = 30; var jsonObj = js.Serialize(theCustomer); context.Response.Write(jsonObj); }
6、定义Customer类
public class Customer { public string Name { get; set; } public int Age { get; set; } public Customer(){} }
相关文章推荐
- 一个轻量级 Javascript 模板引擎 front.js【二】
- 发布一个开源极致的javascript模板引擎tpl.js
- Epii.js 一个极其简单的Js模板引擎
- Juicer – 一个Javascript模板引擎的实现和优化
- 一个周末掌握IT前沿技术之node.js篇<六>:Node.js与客户端模板引擎
- JavaScript/Ajax/JQuery知识点(BOM/DOM/ScriptEngine/JS引擎)
- 使用javascript原生实现一个模板引擎
- 教你使用javascript简单写一个页面模板引擎
- 推荐一个模板引擎 - templateengine
- 说说如何用js实现一个模板引擎
- Javascript模板引擎mustache.js详解
- 只有20行Javascript代码!手把手教你写一个页面模板引擎
- 说说如何用js实现一个模板引擎
- Javascript模板引擎-Surge.js
- 一个Javascript模板引擎的实现和优化
- 如何选择Javascript模板引擎(javascript template engine)?
- doT.js——前端javascript模板引擎问题备忘录
- 使用javascript原生实现一个模板引擎
- Juicer – 一个Javascript模板引擎的实现和优化
- doT.js——前端javascript模板引擎问题备忘录