The CodeTemplate directive
Every CodeSmith Generator template starts with exactly one CodeTemplate directive. It's the only required directive, and it sets the general properties of the template, such as the language the template code is written in and a description. The only thing that can appear before it is one or more comments.
Here's the CodeTemplate directive from the SortedList.cst sample template:
<%@ CodeTemplate Language="VB" TargetLanguage="VB" Description="Generates a strongly-typed collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index." %>
This directive says that the template code is written in Visual Basic, that the template produces Visual Basic output, and what the template is for.
Attributes
Language is required; all other attributes are optional.
| Attribute | Description | Required |
|---|---|---|
Language |
The language the template code is written in: C#, VB (Visual Basic .NET) or JS (JScript). |
Yes |
TargetLanguage |
The language of the template's output. You can use any string. Generator doesn't use it to produce the output, but the Template Editor uses it to syntax-highlight the static content of the template. | No |
Description |
A general description of the template. | No |
Inherits |
The class the template inherits from. See Inherits. | No |
Src |
A source file to compile together with the template. Use it to enable the code-behind model. | No |
Debug |
Whether to include debug symbols in the compiled template assembly. See Debugging templates. | No |
LinePragmas |
Whether to generate line pragmas when the template is compiled. When True, template errors point to the template source code; when False, they point to the compiled source code. |
No |
ResponseEncoding |
The encoding of the template content and its output. Accepts the names supported by System.Text.Encoding.GetEncoding. Output is UTF-8 unless you set this attribute. |
No |
OutputType |
Where the template output goes: Normal, Trace or None. See OutputType. |
No, defaults to Normal |
NoWarn |
A comma-delimited list of compiler warning numbers to suppress. These are the standard C# and VB compiler warning numbers. | No |
ClassName |
The class name of the compiled template. Set it when you use partial code-behind classes. | No |
Namespace |
The namespace of the compiled template. Set it when you use partial code-behind classes. | No |
Encoding |
The encoding used to save the template output and the template document. Template documents are UTF-8 by default. | No |
Note
If a generated file already exists on disk and the regenerated content is identical, Generator doesn't change the file's encoding.
Inherits
By default, every template inherits from CodeSmith.Engine.CodeTemplate. This class provides the basic functionality of a template, much as the Page class does for an ASP.NET page. Use the Inherits attribute to base a template on a different class.
The class you inherit from must derive, directly or indirectly, from CodeSmith.Engine.CodeTemplate, and Generator must be able to find it. Either add an Assembly directive for the assembly that contains the class, or use the Src attribute to point to its source code.
For example, the BaseTemplates sample project defines two template classes: OutputFileCodeTemplate, which inherits directly from CodeTemplate, and SqlCodeTemplate, which inherits from OutputFileCodeTemplate. To base a template on SqlCodeTemplate, start it with these directives:
<%@ CodeTemplate Language="C#" Inherits="CodeSmith.BaseTemplates.SqlCodeTemplate" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
Your template can then call the helper methods of both classes, such as GetSqlDbType(), IsUserDefinedType() and GetSqlParameterStatements(). Template inheritance is a good way to reuse tested utility methods across many templates without copying code.
Note
The BaseTemplates sample is in your extracted samples folder: ...\CodeSmith Generator\Samples\<version>\Projects\CSharp\BaseTemplates.
OutputType
| Value | Behavior |
|---|---|
Normal |
The default. Output is written to the normal Response stream. |
Trace |
Output is written to the Trace object. |
None |
The template produces no output. Use this for a master template that only renders sub-templates to files and has no output of its own. |