Declaring and Using Properties

The key to making templates flexible and useful is to define properties or metadata. CodeSmith Generator uses properties to customize the generated code. When the user opens a template in Template Explorer, they must supply values for all of the required properties defined in the template before they can generate the code. Note that properties can be defined as optional properties, in which case the user need not supply a value before generating code.

Properties are specified in the template using a Property directive. For example, this directive specifies a property named Key which accepts a string value:

<%@ Property Name="Key" Type="System.String" %

The value that the user enters for the Key property will be inserted into the template output any place that the property name appears surrounded by the special characters <%= %>. For example, consider this template:

<%@ CodeTemplate Language="VB" TargetLanguage="VB">
<%@ Property Name="Key" Type="System.String" %>
' The key is <%= Key %>

If the user enters Ham for the value of the Key property, then the output of this template would be:

' The key is Ham

Properties in CodeSmith Generator can be simple or very complex. You can define enumerated properties that allow the user to choose from a predefined selection of values. You can use CodeSmith Generator 's SchemaExplorer to fill properties from database objects. You can create properties based on the contents of an XML file, or even define your own custom properties complete with custom dialog boxes for user property editing.