Home

Castle Stronghold

Code Formatters

A few code formatters are made available to your NVelocity template so source code can be more nicely presented to the user. The formatters implementation came from manoli.net.

Warning

A few code formatters are made available to your NVelocity template so source code can be more nicely presented to the user. The formatters implementation came from manoli.net.

Quick Note

A few code formatters are made available to your NVelocity template so source code can be more nicely presented to the user. The formatters implementation came from manoli.net.

Using the code formatters

The following entries are available to your NVelocity template:

Context entry nameDescription
csFormats C# code
htmlFormats html/xml code
jsFormats javascript code
tsqlFormats T/SQL code
vbFormats Visual Basic.Net code

The following properties can also be used to control the formatting output.

PropertyDescriptionDefault value
TabSpacesGets or sets the tabs width4
LineNumbersEnables or disables line numbers in outputfalse
AlternateEnables or disables alternating line backgroundfalse
EmbedStyleSheetEnables or disables the embedded CSS style sheetfalse

Warning

The formatters instances are shared by all template transformations. That means that changing the property on an execution will change it for all templates running after that. Thus make sure you reset the properties to a default state before using it.

To use any of the formatters all you need to do is invoke the FormatCode method. For example:


$cs.FormatCode("<p>hello</p>")

Formatting examples

The following are examples of the output of the formatters.

C# Formatting


// Some comment

using System;

[SomeAttribute()]
public class SomeClass : BaseClass
{
    private int id = 1;
    
    public SomeClass()
    {
    }
    
    public int Id
    {
        get { return id; }
        set { id = value; }
    }    
}

Html Formatting


<html>
  <body>
  
  <p class="simple">
    Some content
  </p>
  
  </body>
</html>

JavaScript Formatting


function reset()
{
    window.href = 'somewhere';
}

T/SQL Formatting


CREATE TABLE Customers
(
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [name] [varchar] (50) NULL ,
    [type] [varchar] (10) NULL 
) ON [PRIMARY]
GO

VB.Net Formatting


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim fs As FileStream

    fs = New FileStream("Somefile", FileMode.CreateNew)

    fs.Close()

End Sub
Google
Search WWW Search castleproject.org