我想用asp.net webform 做个模板引擎的demo,目前用NVelocity 和Razor 各做了个小demo。
首先来看下NVelocity。
NVelocity模板文件
1
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
2 < html xmlns ="http://www.w3.org/1999/xhtml" >
3 < head >
4 < title >NVelocity Demo </ title >
5 < link href ="../Css/demo.css" rel ="stylesheet" />
6 < link href ="../Css/common.css" rel ="stylesheet" />
7 </ head >
8 < body >
9 < form action ="ParseVM.aspx" method ="POST" >
10 < div class ="query-form" >
11 当前时间:$Now
12 < br />
13 < table >
14 < tr >
15 < td >新闻ID: < input type ="text" name ="searchID" class ="text" value ="" /></ td >
16 < td >< input type ="submit" name ="submit" value ="查询" ></ td >
17 </ tr >
18 </ table >
19 </ div >
20 </ form >
21 < br >
22 < h2 >新闻列表 </ h2 >
23 < div class ="Artical-list" >
24 #foreach($c in $Articles)
25 #beforeall
26 < table >
27 < tr class ="list-header" >
28 < td >ID </ td >
29 < td >标题 </ td >
30 < td >类别 </ td >
31 < td >发布时间 </ td >
32 </ tr >
33 #odd
34 < tr class ="list-odd" >
35 #even
36 < tr class ="list-even" >
37 #each
38 #set ( $NewID = $c.ID +0 )
39 < td >0$NewID </ td >
40 < td >< a href ="#" >$c.Title </ a ></ td >
41 < td >#if($c.Category=1)国内新闻
42 #elseif($c.Category=2)国际新闻
43 #else 其他#end </ td >
44 < td >$c.PulishTime.ToString("yyyy-MM-dd") </ td >
45 #after
46 </ tr >
47 #afterall
48 </ table >
49 #nodata
50 No contacts found!
51 #end
52 </ div >
53 </ body >
54 </ html >
2 < html xmlns ="http://www.w3.org/1999/xhtml" >
3 < head >
4 < title >NVelocity Demo </ title >
5 < link href ="../Css/demo.css" rel ="stylesheet" />
6 < link href ="../Css/common.css" rel ="stylesheet" />
7 </ head >
8 < body >
9 < form action ="ParseVM.aspx" method ="POST" >
10 < div class ="query-form" >
11 当前时间:$Now
12 < br />
13 < table >
14 < tr >
15 < td >新闻ID: < input type ="text" name ="searchID" class ="text" value ="" /></ td >
16 < td >< input type ="submit" name ="submit" value ="查询" ></ td >
17 </ tr >
18 </ table >
19 </ div >
20 </ form >
21 < br >
22 < h2 >新闻列表 </ h2 >
23 < div class ="Artical-list" >
24 #foreach($c in $Articles)
25 #beforeall
26 < table >
27 < tr class ="list-header" >
28 < td >ID </ td >
29 < td >标题 </ td >
30 < td >类别 </ td >
31 < td >发布时间 </ td >
32 </ tr >
33 #odd
34 < tr class ="list-odd" >
35 #even
36 < tr class ="list-even" >
37 #each
38 #set ( $NewID = $c.ID +0 )
39 < td >0$NewID </ td >
40 < td >< a href ="#" >$c.Title </ a ></ td >
41 < td >#if($c.Category=1)国内新闻
42 #elseif($c.Category=2)国际新闻
43 #else 其他#end </ td >
44 < td >$c.PulishTime.ToString("yyyy-MM-dd") </ td >
45 #after
46 </ tr >
47 #afterall
48 </ table >
49 #nodata
50 No contacts found!
51 #end
52 </ div >
53 </ body >
54 </ html >
加载和显示模板
1
public
partial
class ParseVM : System.Web.UI.Page
2 {
3 protected void Page_Load( object sender, EventArgs e)
4 {
5 VelocityEngine engine = new VelocityEngine();
6
7 ExtendedProperties prop = new ExtendedProperties();
8 // 定义模板加载根路径
9 prop.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath( " ~/ "));
10 prop.AddProperty(RuntimeConstants.ENCODING_DEFAULT, " utf-8 ");
11 prop.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);
12 engine.Init(prop);
13
14 Template template = engine.GetTemplate( " Template/demo.vm ");
15 VelocityContext context = new VelocityContext();
16
17 var list = Article.GetArticleList();
18 string searchID = Request.Form[ " searchID "];
19 int id = 0;
20
21 if (searchID != null && int.TryParse(searchID, out id)) {
22 context.Put( " Articles ", list.Where(p => p.ID == id));
23 }
24 else {
25 context.Put( " Articles ", list);
26 }
27
28 context.Put( " Now ", DateTime.Now);
29
30 template.Merge(context, this.Response.Output);
31 }
32
33
34 }
2 {
3 protected void Page_Load( object sender, EventArgs e)
4 {
5 VelocityEngine engine = new VelocityEngine();
6
7 ExtendedProperties prop = new ExtendedProperties();
8 // 定义模板加载根路径
9 prop.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath( " ~/ "));
10 prop.AddProperty(RuntimeConstants.ENCODING_DEFAULT, " utf-8 ");
11 prop.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);
12 engine.Init(prop);
13
14 Template template = engine.GetTemplate( " Template/demo.vm ");
15 VelocityContext context = new VelocityContext();
16
17 var list = Article.GetArticleList();
18 string searchID = Request.Form[ " searchID "];
19 int id = 0;
20
21 if (searchID != null && int.TryParse(searchID, out id)) {
22 context.Put( " Articles ", list.Where(p => p.ID == id));
23 }
24 else {
25 context.Put( " Articles ", list);
26 }
27
28 context.Put( " Now ", DateTime.Now);
29
30 template.Merge(context, this.Response.Output);
31 }
32
33
34 }
然后看下 Razor。
Razor模板文件
1
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
2 < html xmlns ="http://www.w3.org/1999/xhtml" >
3 < head >
4 < title >Razor Demo </ title >
5 < link href ="../Css/demo.css" rel ="stylesheet" />
6 < link href ="../Css/common.css" rel ="stylesheet" />
7 </ head >
8 < body >
9 < form action ="ParseRazor.aspx" method ="POST" >
10 < div class ="query-form" >
11 当前时间: <% = DateTime.Now %>
12 < br />
13 < table >
14 < tr >
15 < td >新闻ID: < input type ="text" name ="searchID" class ="text" value ="" /></ td >
16 < td >
17 < input type ="submit" name ="submit" value ="查询" ></ td >
18 </ tr >
19 </ table >
20 </ div >
21 </ form >
22 < br >
23 < h2 >
24 新闻列表 </ h2 >
25 < div class ="Artical-list" >
26 < table >
27 < tr class ="list-header" >
28 < td >ID </ td >
29 < td >标题 </ td >
30 < td >类别 </ td >
31 < td >发布时间 </ td >
32 </ tr >
33 @for (int i = 0; i < @Model .Count; i++) {
34 var article = @Model[i];
35 if (i % 2 == 1) {
36 @:<tr class ="list-odd" >
37 }
38 else {
39 @: < tr class ="list-even" >
40 }
41
42 < td >0@(article.ID) </ td >
43 < td >< a href ="#" >@article.Title </ a ></ td >
44 < td >@if (article.Category == 1) {
45 @:国内新闻
46 }
47 else if (article.Category == 2) {
48 @:国际新闻
49 }
50 else {
51 @:其他
52 }
53 </ td >
54 < td >@article.PulishTime.ToString("yyyy-MM-dd") </ td >
55 }
56 </ :tr >
57 </ table >
58 </ div >
59 </ body >
60 </ html >
2 < html xmlns ="http://www.w3.org/1999/xhtml" >
3 < head >
4 < title >Razor Demo </ title >
5 < link href ="../Css/demo.css" rel ="stylesheet" />
6 < link href ="../Css/common.css" rel ="stylesheet" />
7 </ head >
8 < body >
9 < form action ="ParseRazor.aspx" method ="POST" >
10 < div class ="query-form" >
11 当前时间: <% = DateTime.Now %>
12 < br />
13 < table >
14 < tr >
15 < td >新闻ID: < input type ="text" name ="searchID" class ="text" value ="" /></ td >
16 < td >
17 < input type ="submit" name ="submit" value ="查询" ></ td >
18 </ tr >
19 </ table >
20 </ div >
21 </ form >
22 < br >
23 < h2 >
24 新闻列表 </ h2 >
25 < div class ="Artical-list" >
26 < table >
27 < tr class ="list-header" >
28 < td >ID </ td >
29 < td >标题 </ td >
30 < td >类别 </ td >
31 < td >发布时间 </ td >
32 </ tr >
33 @for (int i = 0; i < @Model .Count; i++) {
34 var article = @Model[i];
35 if (i % 2 == 1) {
36 @:<tr class ="list-odd" >
37 }
38 else {
39 @: < tr class ="list-even" >
40 }
41
42 < td >0@(article.ID) </ td >
43 < td >< a href ="#" >@article.Title </ a ></ td >
44 < td >@if (article.Category == 1) {
45 @:国内新闻
46 }
47 else if (article.Category == 2) {
48 @:国际新闻
49 }
50 else {
51 @:其他
52 }
53 </ td >
54 < td >@article.PulishTime.ToString("yyyy-MM-dd") </ td >
55 }
56 </ :tr >
57 </ table >
58 </ div >
59 </ body >
60 </ html >
加载和显示模板
1
public
partial
class ParseRazor : System.Web.UI.Page
2 {
3 protected void Page_Load( object sender, EventArgs e)
4 {
5 // 模板文件虚拟目录
6 var path = System.IO.Path.Combine(Request.ApplicationPath, " /Template/demo.cshtml ");
7 var type = BuildManager.GetCompiledType(path);
8 var script = Activator.CreateInstance(type) as System.Web.WebPages.WebPage;
9
10
11 var model = Article.GetArticleList();
12 string searchID = Request.Form[ " searchID "];
13 int id = 0;
14
15 var stringWriter = new StringWriter();
16 if (searchID != null && int.TryParse(searchID, out id)) {
17 script.ExecutePageHierarchy( new WebPageContext(
18 new HttpContextWrapper(HttpContext.Current), null, model.Where(p => p.ID == id).ToList()
19 ), stringWriter);
20 }
21 else {
22
23 script.ExecutePageHierarchy( new WebPageContext(
24 new HttpContextWrapper(HttpContext.Current), null, model
25 ),stringWriter );
26
27 }
28
29 Response.Write(stringWriter);
30
31 }
32
33 }
2 {
3 protected void Page_Load( object sender, EventArgs e)
4 {
5 // 模板文件虚拟目录
6 var path = System.IO.Path.Combine(Request.ApplicationPath, " /Template/demo.cshtml ");
7 var type = BuildManager.GetCompiledType(path);
8 var script = Activator.CreateInstance(type) as System.Web.WebPages.WebPage;
9
10
11 var model = Article.GetArticleList();
12 string searchID = Request.Form[ " searchID "];
13 int id = 0;
14
15 var stringWriter = new StringWriter();
16 if (searchID != null && int.TryParse(searchID, out id)) {
17 script.ExecutePageHierarchy( new WebPageContext(
18 new HttpContextWrapper(HttpContext.Current), null, model.Where(p => p.ID == id).ToList()
19 ), stringWriter);
20 }
21 else {
22
23 script.ExecutePageHierarchy( new WebPageContext(
24 new HttpContextWrapper(HttpContext.Current), null, model
25 ),stringWriter );
26
27 }
28
29 Response.Write(stringWriter);
30
31 }
32
33 }
像NVelocity的话可以通过 context.Put() 方法来输出多个变量,但使用Razor的话,如何添加多个变量在前台显示呢?
下面想试试 webform风格的模板,用WebForm自身的引擎。
但还不知道如何下手,大家能否给点建议?