views:

946

answers:

5

We are building an inhouse application which needs to generate HTML files for upload into eBay listings. We are looking to use a template engine to generate the HTML files based on database and static fields that we have pre-defined. The template also needs to have logic capabilities (if-then, foreach, etc).

We have looked at T4 and it looks perfect, but we don't see anything about whether it has the capabilities to be used at runtime, so that a user can create the T4 template, and then the application can "compile" it and generate the final HTML file. Is this possible, and how?

If not, are there other frameworks we should be looking at that has all these capabilities?

+2  A: 

The T4 templates can be compiled with the TextTransform.exe command line tool. You could have your application create a .tt file, then call TextTransform.exe to generate the output.

rally25rs
Doesn't Visual Studio need to be installed on the user's machine with this solution? This is a limitation for us.
Amberite
Mono has a fairly capable text transformation implementation. I would take a look at that if dependencies are an issue.
Sky Sanders
Yes, for now, it's a depedency on Visual Studio. This will change with VS2010 / .NET 4 and the so-called precompiled T4 templates. See here for info: http://www.olegsych.com/2009/09/t4-preprocessed-text-templates/
marc_s
+3  A: 
Lasse V. Karlsen
Wooow O.o I am downloading this right now and I will let you know how it goes. If it does everything you say it does, you'll be an absolute life saver!
Amberite
Edited the answer now, as you said in the email, the "Data" variable doesn't exist, because it is named "data". It's a method parameter, hence the lowercase "d".
Lasse V. Karlsen
"The way the template engine works is that it produces a .cs file and feeds it to the C# compiler" : Wouldn't that also mean that the machine running this needs VisStudio or at least something else installed? Or is there a way that your programatically calling the C# compiler? I didn't think the compiler was shipped with the .net runtime... Although since IIS can compile ASP.NET on the fly, I guess it must be out there somewhere...
rally25rs
It uses codedom, part of the runtime.
Lasse V. Karlsen
@Lasse Wow, I guess I missed it when they added that feature. Thanks!
rally25rs
To get access to the command line external compilers, you need to install the SDK, but the compiler itself is part of the .NET runtime, unless you install the "Client profile runtime", which I think is missing the compiler. Also, to be precise, I don't actually produce a *file* per se, I produce the source code in memory and feed it to the C# compiler class, adding references to assemblies and setting options before I ask it to compile the code. There's no file on disk in any of this, even the assembly is generated in memory.
Lasse V. Karlsen
Great code, works flawlessly. Plus excellent support from Lasse. Kudos!
Amberite
@Lasse: Links do not seem to work any more. How can I get my hand on this code?
epitka
Updated links have been posted. Unfortunately there is no binary repository at the moment, I'll look at posting new ones for that but since the binary was just a build of the trunk, I decided that if I want to bring back binary support, it has to be in a more controlled manner.
Lasse V. Karlsen
Let me know if you have any questions, either by comments here or email to [email protected]
Lasse V. Karlsen
Posted a link to a template I use in a project, just below the repository links, hopefully it will give you some examples of how to do things with this code.
Lasse V. Karlsen
+3  A: 

The assembly that implements T4 text transformation is Microsoft.VisualStudio.TextTemplating.dll, which ships with Visual Studio.

If you want to start from first principles, you need to implement Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost, and pass your implementation of it as an argument to Microsoft.VisualStudio.TextTemplating.Engine.ProcessTemplate(), which will perform the transformation.

This gives you more flexibility than calling out to TextTransform.exe.

However, if your code is a shipping product, it is unclear what the licensing of this assembly is, and whether you have the rights to redistribute it with your application.

Redistributing this assembly would avoid the need for Visual Studio to be installed.

Leon Breedt
Unfortunately this doesn't meet our needs because of the licensing issue, as this product WILL be shipped to end users. Thanks anyways :)
Amberite
There is also an open-source implementation by the Mono project.http://anonsvn.mono-project.com/viewvc/trunk/monodevelop/main/src/addins/TextTemplating/
Filip
A: 

It is completely possible to use T4 at runtime.

Microsoft doesn't really support this scenario in any reasonable way in .NET 3.5. It sounds like .NET 4.0 will have better support from Microsoft.

Mono does provide some support for this scenario in .NET 3.5.

I have proven out this concept successfully with .NET 3.5 with help from the Mono T4 implementation, but an off the shelf solution for this problem for .NET 3.5 would require a ton more effort than I have invested so far.

You can find the Mono T4 implementation here:

http://anonsvn.mono-project.com/viewvc/trunk/monodevelop/main/src/addins/TextTemplating/

I have documented some of the issues I encountered trying to run T4 templates from .NET code here:

Options for running T4 templates from .NET code

Michael Maddox
+3  A: 

If you can use Visual Studio 2010 for template creation and editing, then you can use precompiled templates, which were designed for exactly this scenario and are the supported option from Microsoft.

You design the template in Visual Studio, precompile it and deploy an assembly that has no dependencies on Visual Studio along with your application.

http://www.olegsych.com/2009/09/t4-preprocessed-text-templates/

GarethJ