codesmith模板主要分三大部分:声明区、模板编辑区、函数编辑区。
首先是声明区:
CodeTemplate声明:
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." ResponseEncoding="UTF-8" %>
Property声明:
<%@ Property Name="SampleStringProperty" Type="System.String" Default="SomeValue" Optional="True" Category="Strings" Description="This is a sample string property." %>
Assembly声明:(类似于net中的引用程序集)
<%@ Assembly Name="SchemaExplorer" %>
Import声明:(类似于Net中的using语句)
<%@ Import Namespace="SchemaExplorer" %>
Register声明:(包含其他模板)
<%@ Register Name="HeaderTemplate" Template="header.cst" %>
第二部分是模板编辑区:
这个就不多说了,直接写就行了。静态的直接写,动态的用<%%>
第三部分是函数编辑区:
所有的函数一定要写在<script runat="template"></script>区域中。如下:
<script runat="template">
// My methods here.
public string SampleMethod()
{
return "Method output.";
}
</script>
然后再模板编辑区中的<%%>里就可以直接使用了。
最后,要在模板中使用数据库中的表的话,请指定一个获取表的参数,如下:
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
这样在执行时指定表,在模板代码中就可以直接通过this.SourceTable来访问相关表的信息了。
2010.11.21