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.
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.
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 name | Description |
| cs | Formats C# code |
| html | Formats html/xml code |
| js | Formats javascript code |
| tsql | Formats T/SQL code |
| vb | Formats Visual Basic.Net code |
The following properties can also be used to control the formatting output.
| Property | Description | Default value |
| TabSpaces | Gets or sets the tabs width | 4 |
| LineNumbers | Enables or disables line numbers in output | false |
| Alternate | Enables or disables alternating line background | false |
| EmbedStyleSheet | Enables or disables the embedded CSS style sheet | false |
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