Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CodeSmith CodeTemplate object


May 25, 2021 CodeSmith


Table of contents


CodeTemplate object

When using code templates to generate code, the CodeSmith engine uses a number of objects behind it to help generate code, most commonly used

  • CodeTempate (page class Asp.Net similar to the code)
  • Progress is used to show the progress of code generation
  • CodeTemplateInfo can return some information about the current template itself.

This article describes CodeTemplate, which represents code template objects handled by the CodeSmith engine and can interact directly with the CodeSmith engine through codeTemplate objects, such as:

  • Use GetFileName to modify the default file name generated by the template
  • Use Render method to output the template to multiple files
  • The code is inserted into the CodeSmith engine to process the template through events.
  • Write directly in the output file through the Response property.

Use GetFileName to modify the file name of the template output

In the previous example CodeSmith used tutorial (2): To write the first code template, we've modified the output file name with GetFileName, for example, defining a ClassName property in your template, and you can change the default file name of the template output to class name through GetFileName

<%@ 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>

Overload the Parse DefaultValue method

When you define the default values for a property, sometimes the default values for some properties may not be converted from String, and you can do so by overloading the ParseDefaultValue method, which is called when each property is processed in the CodeSmith engine, and if you overload the method, you can follow your own logic to handle the default value of the property.

Overload the Render method

CodeTemplate's Render method is called when the CodeSmith engine generates the final output, and you can modify the contents of the output or write the output to multiple files by overloading it. For example, in addition to generating the default output, the following code writes the output to two other files:

<%@ 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>

CodeSmith CodeTemplate object

Note that the base of the base class is called. R ender, otherwise you won't output to the default file. This example Downloads

The template event

The CodeTemplate class defines the following events in which you can add automatic event handling when these events occur.

Use the Response object

As Asp.Net Page objects, you can write directly to the output stream through CodeTemplate's Response property. Like what

<%@ 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>

Write two lines of text directly into the output stream. Response objects are typed codeTemplateWriter classes and are commonly used in the:

  • AddTextWriter - Add additional output locations
  • Indent - Add an indentation to the output
  • Unindent - Reduces one indentation for the output
  • Write - Write
  • WriteLine - Write content and add line breaks