OutputFileCodeTemplate

To base a template on the OutputFileCodeTemplate class, inherit from it in the template's CodeTemplate directive:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Inherits="CodeSmith.BaseTemplates.OutputFileCodeTemplate" Description="Build custom access code." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>

The class does two things:

  • It adds an optional OutputFile property, in the Output category of the property grid. The property shows a Save dialog so the user can pick a file.
  • It overrides the OnPostRender method. After CodeSmith Generator renders the template, the output is written to OutputFile as UTF-8, if a file was selected.

Customizing the Save dialog

To change the Save dialog, override the OutputFile property in your template and apply a FileDialog attribute. For example, to make the user pick a .cs file:

<script runat="template">
// Override the OutputFile property and assign our specific settings to it.
[FileDialog(FileDialogType.Save, Title="Select Output File", Filter="C# Files (*.cs)|*.cs", DefaultExtension=".cs")]
public override string OutputFile
{
    get {return base.OutputFile;}
    set {base.OutputFile = value;}
}
</script>

FileDialog is defined in CodeSmith.CustomProperties, so add these directives to your template as well:

<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>