Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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:

Code Block
languagecsharp
<%@ 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>
Note

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

...