Thursday, 12 January 2012

C# T4 Templating Engine

VS2010 build in templating engine.

Add new ‘Preprocessed Text Template’ called T4Template.tt to your project that looks like that

<#@ template debug="true" language="C#"#>
<#@ parameter name="FirstName" type="System.String" #>
Now: <#= DateTime.Now.ToString() #>
FirstName: <#= FirstName #>
This is a sample program to run the template
class Program
{
    private static void Main(string[] args)
    {
        var template = new T4Template();
        template.Session = new Dictionary<string, object>();
        template.Session.Add("FirstName", "John");
        template.Initialize();
        Console.WriteLine(template.TransformText());
    }
}

and output

Now: 12/01/2012 10:42:07
FirstName: John

No comments:

Post a Comment