TextTemplate Directives

TextTemplate supports a few directives to control the template compilation.

A directive is written inside a special code block:

<%@ directive parameter %>

These directives can be placed anywhere in the template, but will be collected and processed as though they were placed at the very beginning of the template.

The using directive

The using directive is used to specify the namespaces to import into the code, before compiling it. For C#, this is the "using XYZ" directives near the top of the file, and for VB.NET, this is the "Import XYZ" directives near the top of the file.


<%@ using System.Drawing %>
<%@ using System.Collections.Generic %>

  • use
  • using
  • import

The reference directive

The reference directive is used to reference additional assemblies which are needed to compile and execute the template.


<%@ reference System.Drawing %>
<%@ reference System.Design %>

  • ref
  • reference
  • references

The language directive

The language directive is used to specify the language used in the template. This dictates which compiler will be used when compiling it.

Languages known by the TextTemplate class library out of the box:
  • C#v3.5
  • C#v4.0
  • VBv3.5
  • VBv4.0


<%@ language C#v3.5 %>
<% for (int index = 1; index <= 5; index++) { %>
  <%= index %>
<% } %>

You can also extend this list by adding support for your own languages. See the page on Extending language-support for more information.

Last edited Jan 20, 2011 at 10:32 PM by lassevk, version 8


No comments yet.