CodeSmith Generator uses the GetFileName method to provide a default output file name for the template when it's called from the CodeSmith Generator Console Application, Template Editor or Master Template. This is also used in CodeSmith Generator as the default file name if you save the output of a template, and anywhere else that CodeSmith Generator needs to assign a filename to the output of your template. You can override this method in your code when you want to build the default file name based on property input or other factors.
For example, if your C# language template contains a property named ClassName, you might include this code to set the default output file name:
Code Block |
---|
|
<%@ 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> |
Example
Using the template defined below we will show off how changing the database table we generate off of, changes the the file path that the template is rendered to.
Code Block |
---|
|
<%@ Template 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> |
The first step is to create the template above or download the one attached below. Next, choose a table your wish to generate against by configuring the SourceTable Property via the PropertyGrid. In the screenshot below we are choosing a random database table called Account. Finally, we click Generate to render the templates contents to a file called Account.cs file name.
Image Added
Info |
---|
You can download this template by clicking here. |