Friday, June 30, 2006

Ordering Category in CodeSmith

When I worked on a CodeSmith template (*.cst), I came into a small problem to order categories in a property grid. The template I created has several categories and I want it to be displayed in a specific order, not ordered alphabetically as per default.

I came across this discussion that gave me a nice trick to order the categories. We need to prefix the category name with a special character that does not get displayed by Property Grid. Aab character (\t) will do the trick. Multiple tab characters might also be used. So for example if I want to order my categories in the following order:

Context
Persister
BusinessEntity
UnitTests

Then in CodeSmith template I need to write like:

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="\t\t\tContext" %>
<%@ Property Name="CreateBusinessEntity" Type="System.Boolean" Category="\t\tBusinessEntity" Default="True" %>
<%@ Property Name="CreatePersister" Type="System.Boolean" Category="\tPersister" Default="True" %>
<%@ Property Name="CreatePersisterUnitTest" Type="System.Boolean" Category="UnitTest" Default="True" %>

No comments: