Declaring and using properties
Properties make templates flexible: they're the input that CodeSmith Generator uses to customize the generated code. When someone runs a template, they supply values for its properties first. Generator won't generate until every required property has a value; optional properties can be left empty.
You declare a property with a Property directive. This directive declares a property named Key that accepts a string:
<%@ Property Name="Key" Type="System.String" %>
Wherever the template contains <%= Key %>, Generator writes the property's value to the output. For example:
<%@ CodeTemplate Language="VB" TargetLanguage="VB" %>
<%@ Property Name="Key" Type="System.String" %>
' The key is <%= Key %>
If the user enters Ham for Key, the output is:
' The key is Ham
In this section
| Page | What you learn |
|---|---|
| Property directive | Every attribute of the Property directive, and how to declare properties in code instead. |
| Declaring an enumerated property | Limit a property to a fixed set of choices. |
| Property validation | Check optional values and report your own validation errors. |
Other kinds of properties
Properties can be as simple or as rich as you need:
- Fill properties from database objects with SchemaExplorer.
- Base properties on the contents of an XML file.
- Use the editors in CodeSmith.CustomProperties, or build custom metadata sources with their own editors.