JS模板引擎jTemplates 使用笔记(一)
1 引擎简介
jTemplates是一个基于Jquery的js模板引擎插件。该引擎全部代码由JS实现,可以配合AJAX,JSON一起协同工作,模板内容可以用JS代码,实现了活动更新,可以自动从服务器更新模板生成的内容。
jTemplates能免费应用于商业和非商业。
2 下载引用
最新版本号:0.7.8
示例包:http://jtemplates.tpython.com/jTemplates.zip
压缩后:http://jtemplates.tpython.com/jTemplates/jquery-jtemplates.js
未压缩:http://jtemplates.tpython.com/jTemplates/jquery-jtemplates_uncompressed.js
3 简单示例
本Demo使用MVC框架搭建。
I:使用*.ASCX搭建模板内容
主要代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { #template MAIN} <div id= "header" >{$T.Name}</div> <table> { #foreach $T.Peoples as record} { #include ROW root=$T.record} { #/for} </table> { #/template MAIN} { #template ROW} <tr bgcolor= "{#cycle values=['#AAAAEE','#CCCCFF']}" > <td>{$T.FirstName.bold()}</td> <td>{$T.Age}</td> <td>{$T.Email.link( 'mailto:' +$T.Email)}</td> </tr> { #/template ROW} |
II: 使用*.Aspx搭建展示页面
主要代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <script src= "<%=Url.Content(" ~/Scripts/jquery-jtemplates.js ")%>" type= "text/javascript" ></script> <script type= "text/javascript" > $(document).ready(function () { $.ajax({ type: "POST" , url: "<%= Url.Action(" GetData ") %>" , data: "" , contentType: "application/json; charset=utf-8" , cache: false , dataType: "json" , success: function(data){ if (data){ $( "#jTemplateDemo" ).setTemplateURL( "<%=Url.Action(" GetTemplate ")%>" ); $( "#jTemplateDemo" ).processTemplate(data); } } }); }); </script> <div id= "jTemplateDemo" ></div> |
III:Action和Model
Action主要代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public ActionResult Index( string id) { return View(); } /// <summary> /// Request for template content /// </summary> /// <returns>templates</returns> public ActionResult GetTemplate() { return PartialView( "TableTemp" ); } /// <summary> /// Request for temp data /// </summary> /// <returns>temp data</returns> [HttpPost] public ActionResult GetData() { var model = new TempModel{ Name = "New Employees" , Peoples = new List<person> { new Person {ID = "1" , FirstName = "Anne" , Email = "anne@domain.com" ,Age=20}, new Person {ID = "2" , FirstName = "Amelie" , Email = "amelie@domain.com" ,Age=22}, new Person {ID = "3" , FirstName = "Polly" , Email = "polly@domain.com" ,Age=35}, new Person {ID = "4" , FirstName = "Alice" , Email = "alice@domain.com" ,Age=27}, new Person {ID = "5" , FirstName = "Martha" , Email = "martha@domain.com" ,Age=32} } }; return Json(model); }</person> |
Model主要代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Person { public string ID { get ; set ; } public string FirstName { get ; set ; } public string Email { get ; set ; } public int Age { get ; set ; } } public class TempModel { public string Name { get ; set ; } public List<Person> Peoples { get ; set ; } } |
3 运行效果图
■ 拿起勇气,探索你未知的世界。
分类:
Javascript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!