---
title: "CodeSmith 使用教程(8): CodeTemplate对象"
author: "mapdigit"
date: "2013-01-14T10:25:00Z"
tags: []
slug: "codesmith-8-codetemplate"
draft: false
lastmod: "2013-03-22T14:03:27Z"
id: 1834176
url: "https://mapdigit.iteye.com/blog/1834176"
---

在使用代码模板产生代码时，CodeSmith引擎背后使用了不少对象来帮助代码的生成，其中常用的有

- CodeTempate （类似于Asp.Net的Page类）
- Progress 用于显示代码生成的进度
- CodeTemplateInfo 可以返回关于当前模板自身的一些信息。

本篇介绍CodeTemplate ，CodeTemplate代表了由CodeSmith引擎处理的代码模板对象，可以通过CodeTemplate对象直接和CodeSmith引擎交互，比如：

- 使用[GetFileName](http://docs.codesmithtools.com/display/Generator/Overriding+the+GetFileName+Method)修改模板生成的缺省文件名
- 使用[Render method](http://docs.codesmithtools.com/display/Generator/Overriding+the+Render+Method)把模板的输出到多个文件中
- 通过[events](http://docs.codesmithtools.com/display/Generator/Template+Events)把代码插入到CodeSmith引擎处理模板的过程中。
- 通过[Response](http://docs.codesmithtools.com/display/Generator/The+Response+Property)属性直接在输出文件中写内容。

使用[GetFileName](http://docs.codesmithtools.com/display/Generator/Overriding+the+GetFileName+Method)修改模板输出的文件名

在前面的例子[CodeSmith 使用教程(2): 编写第一个代码模板](http://www.imobilebbs.com/wordpress/archives/4182)我们已经使用GetFileName修改过输出的文件名，比如在你的模板中定义了一个ClassName属性，可以通过[GetFileName](http://docs.codesmithtools.com/display/Generator/Overriding+the+GetFileName+Method)把模板输出的缺省文件名改成类名

```
<%@ Template Language="C#" TargetLanguage="Text" %>
<%@ Property Name="ClassName" Type="System.String" Default="ClassName" %>

This template shows off how to override the GetFileName method.

<script runat="template">
public override string GetFileName()
{
    return ClassName + ".cs";
}
</script>
```

重载ParseDefaultValue方法  
在定义属性的缺省值时，有时有些属性的缺省值可能无法从String转换，此时可以通过重载ParseDefaultValue方法，这个方法会被CodeSmith引擎中处理每个属性时调用，如果你重载了这个方法，可以按照你自己的逻辑来处理属性的缺省值。

重载Render方法  
CodeTemplate的Render方法是CodeSmith引擎生成最终输出时调用的，可以通过重载这个方法来修改输出的内容或是把输出写到多个文件中。  
比如下面代码除了生成缺省的输出外，还把输出写到另外两个文件中：

```
<%@ CodeTemplate Language="C#" TargetLanguage="Text"
   Description="AddTextWriter Demonstration." %>
<%@ Import Namespace="System.IO" %>
//This template demonstrates using the AddTextWriter method
//to output the template results to multiple locations concurrently.
<script runat="template">
public override void Render(TextWriter writer)
    {
        StreamWriter fileWriter1 = new StreamWriter(@"test1.txt", true);
        this.Response.AddTextWriter(fileWriter1);

        StreamWriter fileWriter2 = new StreamWriter(@"test2.txt", true);
        this.Response.AddTextWriter(fileWriter2);

        base.Render(writer);

        fileWriter1.Close();
        fileWriter2.Close();
    }
</script>
```

[![20130105002](http://www.imobilebbs.com/wordpress/wp-content/uploads/2013/01/20130105002.png)](http://www.imobilebbs.com/wordpress/archives/4213/attachment/20130105002)注意调用基类的base.Render，否则你就不会输出到缺省的文件。本例[下载](http://www.imobilebbs.com/download/codesmith/RenderDemo.zip)

模板事件

CodeTemplate类定义了下面几个事件，你可以中这些事件发生时添加自动的事件处理.

- [OnInit](http://docs.codesmithtools.com/display/Generator/The+OnInit+Event)事件帮助中代码模板创建时
- [OnPreRender](http://docs.codesmithtools.com/display/Generator/The+OnPreRender+Event)事件发生在准备写输出文件前
- [OnPostRender](http://docs.codesmithtools.com/display/Generator/The+OnPostRender+Event)事件发生在准备写输出文件后
- [OnPropertyChanged](http://docs.codesmithtools.com/display/Generator/The+OnPropertyChanged+Events)事件发生在属性值发生变化时.

使用Response对象

和Asp.Net的Page对象一样，可以通过CodeTemplate的Response属性直接在输出流中写入内容。比如

```
<%@ CodeTemplate Language="C#" TargetLanguage="Text"
Description="This template demonstrates writing directly to the Response property" %>
<% RenderDirect(); %>
<script runat="template">
public void RenderDirect()
{
Response.WriteLine("Written directly to the Response property.");
Response.WriteLine("Hello " + System.Environment.UserName + "!");
}
</script>
```

直接在输出流中写入两行文字。Response对象的类型为CodeTemplateWriter类 ，常用的方法有：

- AddTextWriter -添加额外的输出位置
- Indent – 为输出添加一个缩进
- Unindent -为输出减少一个缩进
- Write -写入内容
- WriteLine -写入内容并添加分行符