Versions Compared

Key

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

The key to making templates flexible and useful is to define properties or metadata. CodeSmith uses properties to customize the generated code. When the user opens a template in CodeSmith 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:

...

<%@ 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:

...

Properties in CodeSmith 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'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.