关于Nvelocity的主要语法和一些代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
context.Response.ContentType = "text/html";
           VelocityEngine vltEngine = new VelocityEngine();
           vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
           vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
           vltEngine.Init();
 
           //匿名类 把类的定义和对象的声明初使化放到一起
           var news = new { Title = "ffff", Author = "AL", PostDate = "2013-11-8", Msg = "公布消息细节" };
 
 
            
           VelocityContext vltContext = new VelocityContext();
           vltContext.Put("people", news);//设置参数,在模板中可以通过$data来引用
 
           Template vltTemplate = vltEngine.GetTemplate("displayNews.htm");
           System.IO.StringWriter vltWriter = new System.IO.StringWriter();
           vltTemplate.Merge(vltContext, vltWriter);
 
           string html = vltWriter.GetStringBuilder().ToString();
           context.Response.Write(html); //输出html代码

  下面是Html里的模板引擎的语法写法 和C#很相似

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
34
35
36
37
38
39
40
41
42
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
#parse("head.htm")
 
$ps.tom
 
1:
<ul>
#foreach($mr in $MR)
<li>$mr</li>
#end
</ul>
2:
<ul>
    #foreach($prs in $persons)
    <li>$prs.Name 年龄是 $prs.Age</li>
    #end
</ul>
 
#if($age>10)
    大于10
#else
小于等于10
#end
 
3:
<ul>
#foreach($prs in $persons)
    #if($prs.Age>20) 
      <li style="color:Red">$prs.Name的年龄是$prs.Age</li>
      #else
        <li style="color:Black">$prs.Name的年龄是$prs.Age</li>
      #end
#end
</ul>
#parse("foot.htm")
</body>
</html>

  

posted @   KyrieYang  阅读(1596)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
点击右上角即可分享
微信分享提示