CodeSmith Generator uses the GetFileName method to provide a default output filename for 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 table we generate off of changes the the 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 and then specify a table to generate against by setting the SourceTable Property via the PropertyGrid. In the screenshot below we are choosing a random database table called Account. Once we click Generate a file will be created with the Account.cs file name.
Image Added
Info |
---|
You can download this template by clicking here. |