OMS.Ice - T4 Text Template Generator – Documentation

This documentation will show you how to create a T4 text template and how to generate textual output based on a T4 text template at runtime.

Code and Text Generation by using T4 Text Templates

Design-Time Code Generation

If you need to generate program code or other textual files within your Visual Studio project, you typically write a template that can be used to generate code from a model. If the model changes, you just need to regenerate the code.

The design-time code generation is well integrated into the Visual Studio.

Run-time Text Generation by using Preprocessed T4 Text Templates

To generate text output at run-time, you can add a preprocessed text template to your Visual Studio project. Visual Studio automatically generates a code-behind file for the template. The generated code file contains the source code for a text generator class that can be used from your application by calling the TransformText method.  Whenever you change the template, Visual Studio regenerates the subsidiary code file.

There is a drawback with using preprocessed T4 text templates. The code for the text generator will be created at design-time. At run-time you can only pass data to the generator and the text output has a fix structure. The user cannot customize the template to his own needs.

Run-time Text Generation by using T4 Text Templates

In applications where you need customizable T4 text templates or where the user shall be able to create its own templates, you cannot use preprocessed T4 text templates. In more complex scenarios you may even have a meta template that can be used to generate different T4 text templates.
There are many other use cases where you need more flexibility than given with the Visual Studio T4 text generation.

The OMS.Ice – T4 Text Template Generator is all in one, a T4 text template parser, compiler and generator. At runtime, the generator parses a T4 text template, builds code in the selected language (C# or VB), compiles that code into memory, and finally executes the text generator.


Writing T4 Text Templates

A T4 text template is a mixture of meta data, text, and control logic and consists of the following parts:


A directive determines how the template will be processed.

Text Blocks

Text blocks are content that is directly written to the output.

Control Blocks

Control blocks are program code written in C# or VB.The control logic defines how the text will be written to the output. Based on conditions, the text can be ignored or repeated. Control blocks can also provide dynamic text  based on external data or program logic.

Using OMS.Ice – T4 Text Template Generator

More …

Last edited Apr 25, 2012 at 8:31 AM by olafmeyer, version 4


No comments yet.