CodeSmith使用基礎教程 (3) — 語法與對象

五、CodeSmith的模版中的語法

代碼標籤
       <% %>
標籤
       可以放置任意數量的代碼在其中,但並不能直接輸出到模版中。
 
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
<%= column.Name %>
<% } %>

<%= %>標籤

在模版中輸出一個字符串。上例中的<%=column.Name%>

腳本標籤

在這個標籤中可以包含一段代碼,但是他不直接影響輸出的模版。可以放置一些比較有幫助的方法在其中,然後在模版的各個地方可以調用它。在腳本標籤中必須包含這個參數runat=”template”,否則他會被處理成普通文本。

例:

 1 <script runat="template">
 2 private string GetColumnName(ColumnSchema cs)
 3 {
 4       return cs.Name;
 5 }
 6 </script>
 7 
 8 <% foreach (ColumnSchema cs in SourceTable.Columns) { %>
 9 <%= GetColumnName(cs) %>
10 <% } %>

 

 

使用標籤可以大量減少代碼,並使模版更加的易讀和一管理。

 

Include標籤

ASP.NET一樣,可以在模版中包含一些文本文件,但同ASP.NET一樣它也不是總能達到你的目標。

例:

<!-- #i nclude file="myfile.inc" -->

 

 

有時在多個模版中引用一個組件中的功能,調用其中的方法,這時我們引用組件。但有些情況下,適用Include標籤可以得到更好的效果。

Comment標籤

註釋標籤,在前邊已經做過介紹。

例:

       CodeSmith Object
       CodeSimth中有許多對象可以在編寫模板的時候使用,這裏將介紹這些對象的一些公用方法和屬性以及怎麼使用它們。

 
六、CodeSmith對象。

代碼模板對象(CodeTemplate Object
在模板中,“this”(或者“Me”在VB.NET中)在當前模板中代碼代碼模板對象。 

代碼模板的方法(CodeTemplate Methods

1
public virtual void GetFileName()
可以重載這個方法設置模板輸出到文件的名稱。否則CodeSmith將基於模板名稱和TargetLanguage設置它的文件名。

2public void CopyPropertiesTo(CodeTemplate target)
這個方法可以實現從一個模板中將其所有屬性的值拷貝到另一個模板所有對應屬性中,並按照相應的屬性值類型進行匹配。

3public object GetProperty(string propertyName)
這個方法將返回一個給定名稱的屬性的值。

4public void SetProperty(string propertyName, object value)
此方法可以根據給定名稱的屬性設置其值。

5public string SavePropertiesToXml ()
這個方法將現有的屬性值保存成一個XML的屬性字符串。

6public void SavePropertiesToXmlFile (string fileName)
這個方法將當前屬性值保存成一個XML的屬性文件。

7public void RestorePropertiesFromXml(string propertySetXml, string baseDirectory)
從保存在XML文件中的屬性字符串,將模板的屬性值恢復。

8public void RestorePropertiesFromXmlFile(string fileName)
從保存在XML文件中的屬性文件,將模板的屬性值恢復。 

代碼模板的屬性(CodeTemplate Properties
Response:此屬性可以訪問當前的TextWriter對象,這個對象是用來輸出模板用的。

CodeTemplateInfo:這個屬性用來訪問當前的CodeTemplateInfo對象,這個對象包含當前模板的一些信息。

Progress:這個屬性用來報告當前模板的執行過程。

 

Response Object
這個對象提供直接寫輸出模板的方法。與ASP.NETresponse對象很相似。下面是一個利用ResponseWrite方法在模板上輸出一段文字的例子。

<% Response.Write("This will appear in the template") %>

IndentLevel (Int32)
當使用Response對象時輸出文本的縮進級別。

Indent() Method
將輸出縮進一個級別。

Unindent() Method
將輸出少縮進一個級別。

AddTextWriter(TextWriter writer) Method
Response對象增加一個TextWriter。這樣可以使在同一時間用多個TextWriter輸出模板。

 

CodeTemplateInfo Object
此對象包含一些當前模板的信息。下面是一些CodeTemplateInfo可用的屬性。

DateCreated (DateTime)
返回一個date類型值,是模板創建的時間。

DateModified (DateTime)
返回模板最後一次被修改的時間。

Description (string)
返回模板聲明時對模版的描述信息。

DirectoryName (string)
返回當前模板文件所在的路徑。

FileName (string)
返回當前模版文件的文件名稱。

FullPath (string)
返回當前模板的完整路徑,路徑名+文件名。

Language (string)
返回代碼模版聲明時使用的語言。

TargetLanguage (string)
返回代碼模版聲明時生成的目標語言。

 

Progress Object

這個屬性用來報告當前模板的執行過程。下面是一些Progress可用的成員。

MaximumValue (Int32)
模版progress允許的最大值。

MinimumValue (Int32)
模版progress允許的最小值。

Step (Int32)
模版每執行一不progress的增長值。

Value (Int32)
Progress的當前值。

PerformStep() Method
按照指定好的progress的增加值執行一步。(原文:Perform a progress step incrementing the progress value by the amount specified in the Step property.

Increment(Int32 amount) Method
指定progress的增加值。(原文:Increment the progress value by the specified amount.

OnProgress (ProgressEventHandler) Event
這個事件用來報告模版的執行過程。(原文:This event can be used to be notified of template execution progress.

1this.Progress.OnProgress += new ProgressEventHandler(this.OnProgress);
2
3public void OnProgress(object sender, ProgressEventArgs e)
4{
5  Trace.WriteLine(e.Value);
6}
<%-- This is a comment --%>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章