Within our <script runat="template"> blocks we begin writing our main worker methods for our template. These methods will be compiled within the template, and will maintain context of the template. So, any Response.WriteLine()'s seen in the following will be displayed on render of the template output.
The first method of interest is PrintTableSummary(). This method takes one argument, DatabaseSchema, and builds a column view of all tables within the given database, building anchors down to anchor names within the same page. This allows a large listing of tables, with a quick way to see all tables represented in the documentation and easy navigation between.
Here's the code:
// How many columns should be displayed in the table summary?
private const int NUM_OF_COLUMNS = 4;
// This method creates a columned display of all tables in the database
// with anchor links down to a detail listing.
public void PrintTableSummary(DatabaseSchema database)
int rowCount = 1;
int currentRow = 1;
int numberTablesPerColumn = database.Tables.Count / NUM_OF_COLUMNS;
bool hasRunOver = ((database.Tables.Count % NUM_OF_COLUMNS == 0) ? false : true);
for (int i = 0; i < database.Tables.Count; i++)
// Are we at the end of our column. If so, close the column.
// Also, take into account if we're at the last column and we need to
// finish out elements.
if (rowCount == numberTablesPerRow &&
(currentRow != NUM_OF_COLUMNS && hasRunOver))
rowCount = 1;
if (rowCount == 1)
Response.WriteLine(" <td valign=\"top\">");
Response.WriteLine(" <img src=\"images/tables_icon.gif\" border=\"0\"> <a href=\"#" +
i + "\">" + database.Tables[i].Name + "</a><br>");
This method takes a constant, NUM_OF_COLUMNS, as the driving factor on how many columns should be represented with your table listing. In our case, we're going to use a basic 4 column listing.
At the first of the method you see that we gather some basic calculations. First, calculating (based on our NUM_OF_COLUMNS), about how many elements should be in each column. The next line considers, based on our NUM_OF_COLUMNS and the number of elements per column, if there's any "run off" (any remaining elements outside of our calculated number of elements). This makes sure that no elements are chopped off at the end, and that all elements are represented within our display.
We then go through some basic looping operations, building the table HTML. The only fairly complex logic here is to determine when to start and stop <td>'s (columns). And, note that we display each table's name with an anchor tag pointing to a anchor name in the same page. We base this anchor just on the table's specific index within the collection. The template assumes that if the table is in a certain index here, it will also be in that same index later in code.
The important CodeSmith specifics here to notice are that our DatabaseSchema object contains a property called Tables, which is a collection of TableSchema objects. We then use the TableSchema's Name property to get the actual table name for the respective object.