Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The CodeTemplate directive is the only required directive and is used to specify the general properties of the template, such as the language that the template is written in and the description. For example, here's the CodeTemplate directive from the VBSortedListSortedList.cst sample template:

Code Block
languagehtml/xml
<%@ 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 specifies that the template uses Visual Basic .NET as its own code-behind language, and that it produces VB output. It also includes a description of the purpose of the template.

CodeTemplate Directive Attributes

There are seven attributes that you can supply to the CodeTemplate directive. The Language parameter is required; all of the rest are optional.

...

Having done this, all of the helper methods defined in the OutputFileCodeTemplate and SqlCodeTemplate classes, such as GetSqlDbType(), IsUserDefinedType(), GetSqlParameterStatements(), and many more, are available to your template. Template inheritance thus provides a good way to reuse tested utility methods across multiple templates without cut-and-paste duplication of code.

Info
Tip: The BaseTemplates sample can be found in your extracted samples ( ...\CodeSmith Generator\Samples\<VERSION>\Projects\CSharp\BaseTemplates )

...

The Debug attribute is used to determine whether or not debug symbols should be included in the generated assembly. Setting this attribute to True will enable you to set break points in your template using the System.Diagnostics.Debugger.Break( ) methodClick here to learn more about debugging.

LinePragmas

The LinePragmas attribute is used to determine whether or not line pragmas are generated during template compilation. When this attribute is set to True, template errors will point to the template source code. If it is set to False, then template errors will point to the compiled source code.

...

Comma-delimited list of the warning ID numbers that the template compiler should suppress. These are standard C# / VB compiler warning ID numbers.

ClassName

The ClassName attribute is used to specify the class name of the compiled template. This attribute should be defined when using partial code-behinds.

Namespace

The Namespace attribute is used to specify the namespace for the compiled template. This attribute should be defined when using partial code

...

languagehtml/xml

...

-behinds.

Encoding

The Encoding attribute allows you to define the encoding the current template document will be saved as. The default encoding of a template document is UTF-8.

ResponseEncoding

The ResponseEncoding attribute allows you to define the encoding the generated document will be saved as. The default encoding of a generated document is UTF-8.

Info
If a generated document already exists on disk and the regenerated documents contents match exactly, the documents encoding will not be changed.