zoukankan      html  css  js  c++  java
  • 关于Nvelocity的主要语法和一些代码示例

     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#很相似

    <!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>
    

      

  • 相关阅读:
    Mysql 执行安装脚本报错Changed limits:
    Centos6.6 安装Mysql集群
    Oracle11g RAC+DG搭建
    Oracle用函数或PIVOT实现行转列
    Oracle根据列中的特殊符号进行分组
    Hadoop on Windows with Eclipse -02- Prerequisites
    Hadoop on Windows with Eclipse -01- Introduction
    Hadoop入门之WordCount运行详解
    Hadoop namenode无法启动问题解决
    jar 打包命令详解
  • 原文地址:https://www.cnblogs.com/yzenet/p/3446682.html
Copyright © 2011-2022 走看看