Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

The CodeTemplate.Render method is where CodeSmith does the actual work of combining metadata with your template to create the template's output. You can override this method if you want to modify the way that CodeSmith ultimately handles that output. For example, overriding this event allows you to write your template's output to multiple destinations instead of just to the default output window. Here's a template that outputs some text to two files at the same time, as well as to CodeSmith's default 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>

Don't omit the call to the base.Render method. If you forget this, then you won't get the default output!

You also have access to the default TextWriter if you override the Render method. This means that you can write your own headers or other additional information directly to the output along with the template's output.

  • No labels