Dotliquid是我新进发现的一个用于.net项目的模板引擎,感觉还不错,使用比较简单,功能非常强大。有兴趣的可以看看https://github.com/formosatek/dotliquid,这里写了2个小例子,大家可以先看看
{{name.Nick}}
对应的解析代码如下
public class DotLiquidController : Controller { // // GET: /DotLiquid/ [AcceptVerbs(HttpVerbs.Post)] public ActionResult DotLiquidDemo1(string template) { ViewData["template"] = Template.Parse(template).Render(Hash.FromAnonymousObject(new { name =new StudentDrop( new Student() { Nick = "chenlei" } )})); return View(); } public ActionResult DotLiquidDemo1() { return View(); } } public class Student { public string Nick { get; set; } } public class StudentDrop :Drop { private readonly Student student; public string Nick { get {return student.Nick;} } public StudentDrop(Student studentPara) { student = studentPara; } }
{% for item in name %} {{ item.Nick }} {% endfor %}
对应的代码如下:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult DotLiquidDemo1(string template) { ViewData["template"] = Template.Parse(template).Render(Hash.FromAnonymousObject(new { name =new List<StudentDrop>(){ new StudentDrop( new Student() { Nick = "chenlei" } )}})); return View(); } public ActionResult DotLiquidDemo1() { return View(); } } public class Student { public string Nick { get; set; } } public class StudentDrop :Drop { private readonly Student student; public string Nick { get {return student.Nick;} } public StudentDrop(Student studentPara) { student = studentPara; } }