Overriding the GetFileName method
CodeSmith Generator calls a template's GetFileName method whenever it needs a file name for the template's output: when the template runs from the console application, from the Template Editor or as a sub-template of a master template, and as the suggested name when you save the output. Override it to build the file name from property values or anything else.
For example, if a C# template has a ClassName property, this override names the output after the class:
<%@ CodeTemplate 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>
Example: naming the output after a table
This template generates a class for a database table and names the output file after the table:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
//This template shows off how to override the GetFileName method using a Database Table.
public class <%= SourceTable.Name %>
{
}
<script runat="template">
public override string GetFileName()
{
return SourceTable.Name + ".cs";
}
</script>
To try it, open the template, select a table for the SourceTable property in the property grid, and generate. If you choose a table named Account, the output is named Account.cs. Choose a different table and the file name changes with it.
Tip
See Default output files in templates for how the console application uses GetFileName.