Overriding the Render method

The CodeTemplate.Render method is where CodeSmith Generator does the actual work of combining metadata with your template to produce the output. Override it to change what happens to that output.

For example, this template sends its output to two files at the same time as the normal output window:

<%@ 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(@"C:\test1.txt", true);
    this.Response.AddTextWriter(fileWriter1);

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

    base.Render(writer);

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

Important

Don't omit the call to base.Render. Without it, you don't get the template's default output.

Your override also receives the default TextWriter, so you can write your own headers or other content directly to the output along with the template's output.

For more about sending output to extra destinations, see Writing to multiple outputs.