ASP.NET 通常處理程序基礎2(Nvelocity模板引擎用法)

Person person = new Person();
person.Name = "yzk";
person.Age = 30;html

Person dad = new Person();
dad.Name = "ywx";
dad.Age = 60;
person.Father = dad;
在html模板中能夠這樣取值<p>個人老爹是 $p.Father.Name,他的年齡是$p.Father.Age </p>post


1九、Nvelocity的特性,若是創建的對象是個索引,還能夠把索引當作它的值。
Dictionary<string,string> dict=new Dictionary<string,string>();//建一個Dictionary鍵值對
dict["tom"]="斯坦福";
dict["jim"]="加里敦";
dict["yzk"]="哈佛"; //把Dictionary扔給模板引擎網站

VelocityEngin vltEngine=new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER,"file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,System,Web.Hosting.HostingEnvironment.MapPath("~/templates"));
vltEngine.Init();
VelocityContext vltContext=new VelocityContext();
vltContext.Put("ps",dict);//這裏改爲使用字典
Template vltTemplate=vltEngine.GetTemplates("test3.htm");
System.IO.StringWriter.vltWriter=new System.IO.StringWriter();
vltTemplate.Merge(vltContext,vltWriter);//用GastleNvelocity1.11版本的就不會報錯了

string html=vltWriter.GetStringBuilder().ToString();
context.Response.Write(html);

--------------------------------test3.htm
<p>$ps.tom;</p> 能夠獲得斯坦福ui

 

20、Nvelocity的深刻,能夠對對象進行foreach
C#基本語法
string[] strs=new string[]{"dsdsds",sresfs"};
foreach(string s in strs)
{
Console.WriteLine(s);
}
-------------------------------能夠相似於這樣的遍歷
string[] strs=new string[]{"黃繼光","古天樂","張靚穎"};
vltContext.Put("mingrens",strs);
-----------------------------------------------
List<Person> persons=new List<Person>();//用前面定義好的那個Person類
persons.Add(new Person{Age=30,Name="林華麗"});
persons.Add(new Person{Age=10,Name="王二小"});
persons.Add(new Person{Age=20,Name="劉德華"});
vltContext.Put("psersons",persons);
...........................
-------------------------test3.htm裏面
<body>
#foreach($mr in $mingrens)
<li>$mr</li>
#end
</body> //這就是對集合進行遍歷而後輸出的方法
----------------------------------------------------------
<body>
#foreach($mr in $mingrens)
<li>$p.Name的年齡是$p.Age</li>
#end
</body> //這就是對集合進行遍歷而後輸出的方法spa


2一、能夠在NVelocity中用if else判斷
#if($age>10)
大於10
#else
小於10
#end
----------------------------------
<ul>
#foreach($p in $persons)
#if($p.Age>20)
<li style="color:Red">$p.Name的年齡是$p.Age</li>
#else
<li style="color:Green">$p.Name的年齡是$p.Age</li>
#end
#end
</ul>code


2二、Nvelocity的包含,相似於母版頁,全部的頁面中都調用這個,包含頭尾,和母版同樣用。
--------------------------------head.htmxml

<!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>
<p><h1>歡迎光臨$age</h1></p>
#if($age>30)
hahaha
#end

-------------------------------foot.htmhtm

#if($age<10)
hahaha
#end
<p>本網站版權全部$age</p>
</body>
</html>

-------------------------------test3.htm
#include("head.htm");
....................
#include("foot.htm");
-------------------------------guanggao.htm能夠加個廣告,不光是頭尾
<a href="http://www.baidu.com">點擊加載
#include("guanggao.htm");
************************************************************************************
除了include還有個叫parse能夠將include替換掉,做用和效果是同樣的,可是有區別。
include元素只是把頁面內容包含進來,並不會解析其中的Nvelocity元素,而parse會解析其中的元素
也就是說早head.htm或者foot.htm中加入$p.name不會include不會解析,parse會解析對象

2三、對Nvelocity進行封裝
用C#中提供的匿名類來解決這個問題
----------------------------------------displayNews.htm
<body>
<h1>$title</h1>
<p>做者:$author;發佈日期:$postdate</p>
<p>$msg</p>
</body>
---------------------------------------DisplayNews.ashx
vltContext.Put("title","sfdsds");
vltContext.Put("author","sfdsds");
vltContext.Put("person","sfdsds");
vltContext.Put("age","sfdsds");//這樣要put四個變量
******************************************怎麼解決
<body>
<h1>$data.Title</h1>
<p>做者:$data.Tuthor;發佈日期:$data.Tostdate</p>
<p>$data.Msg</p>
</body>
-----------------------------------定義匿名類//所謂的匿名類其實就是編譯器自動幫你生成的一
個類,其實仍是一個正常的類
var news=new{Tile="特大喜訊",Author="劉德華",PoseDate="2013-11-08",Msg="今晚上會公佈喜訊
細節!"};//把類的定義和對象的聲明初始化放到一塊兒
string s=news.PostDate;//News是累的一個對象,一個類既然繼承object那麼直接能夠點出來了
匿名類的好處是,隨new隨用,用完就扔了
vltContext.Put("data",news);//把匿名類的對象放進去blog

public void ProcessRequest(HttpContext context)
{
    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 = "特大喜訊",Author="楊中科",PostDate="2013-11-08",Msg="今天晚上會公佈喜訊細節!" }; string s = news.PostDate; VelocityContext vltContext = new VelocityContext(); vltContext.Put("data", news); Template vltTemplate = vltEngine.GetTemplate("displayNews1.htm"); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); string html = vltWriter.GetStringBuilder().ToString(); context.Response.Write(html); }

也能夠定義一個Nvelocity類簡化代碼:

        public static string RenderHtml(string name, object data)
        {
            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();

            VelocityContext vltContext = new VelocityContext();
            vltContext.Put("Model", data);//設置參數,在模板中能夠經過$data來引用

            Template vltTemplate = vltEngine.GetTemplate(name);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            return vltWriter.GetStringBuilder().ToString();
        }
相關文章
相關標籤/搜索