gigi
gigi

Reputation: 3936

How to process T4 string templates?

I have the following question regarding to T4 templates. Considering SomeTemplate.tt containing :

<#@ template language="C#" #>
Today is  <#=  DateTime.Today.ToString() #>

It is processed like this:

 SomeTemplatest st = new SomeTemplate(); 
 string  processedText = st.TransformText();

Q: What is that template is not an existing tt and is somewhere in the database. How can that template, as a string, be processed?

Upvotes: 4

Views: 1221

Answers (1)

Nico
Nico

Reputation: 2110

Despite my comment to your question, in both cases you would need to create an Instance of the Microsoft.VisualStudio.TextTemplating.Engine class. This class has a method ProcessTemplate(...) which you can pass the template code to be transformed as a string.

The second parameter might be more tricky, which is a Template Host providing contextual information and functionality to transform the template. I am not sure if there is a way to get the TextTemplatingEngineHost of Visual Studio.

But for a post-build standalone version (e.g. you want to transform templates within your application) you have to create a custom Text Template Host. A walkthrough how to do so can be found here: http://msdn.microsoft.com/en-us/library/bb126579.aspx

Upvotes: 2

Related Questions