Reputation: 1409
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?
Upvotes: 23
Views: 16206
Reputation: 4318
Liquid might be a good choice for this. This is an open-source template language, read more about the language here: https://shopify.github.io/liquid/
Here is an implementation for .NET: https://github.com/dotliquid/dotliquid
The syntax is quite nice. Here is some sample code for C#:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public List<string> Friends { get; set; }
}
static void Main(string[] args)
{
Template.RegisterSafeType(typeof(Person), new string[]
{
nameof(Person.Name),
nameof(Person.Age),
nameof(Person.Friends),
});
Template template = Template.Parse(
@"<h1>hi {{name}}</h1>
<p>You are{% if age > 42' %} old {% else %} young{% endif %}.</p>
<p>You have {{ friends.size }} friends:</p>
{% assign sortedfriends = friends | sort %}
{% for item in sortedfriends -%}
{{ item | escape }} <br />
{% endfor %}
");
string output = template.Render(
Hash.FromAnonymousObject(
new Person()
{
Name = "James Bond",
Age = 42,
Friends = new List<string>()
{
"Charlie",
"<TagMan>",
"Bill"
}
} ));
Console.WriteLine(output);
/* The output will be:
<h1>hi James Bond</h1>
<p>You are young.</p>
<p>You have 3 friends:</p>
<TagMan> <br />
Bill <br />
Charlie <br />
*/
}
Upvotes: 0
Reputation: 1596
One mistake I made is I added a "Text Template" file. To generate text at runtime, pick the "Preprocessed Text Template" instead. If you originally picked "Text Template", it's an easy change to set the Custom Tool to "TextTemplatingFilePreprocessor" in the file properties in VS.
Upvotes: 0
Reputation: 12489
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:
https://github.com/mono/monodevelop/tree/master/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
Upvotes: 3
Reputation: 391276
I have a similar set of classes that I use for this, embedding templated text generation into software.
Basically, it works like old-style ASP, you surround C# code in <%...%>
blocks, and you can emit results by using <%= expression %>
.
You can pass a single object into the template code, which of course can be any object type you like, or simply an array of parameters. You can also reference your own assemblies if you want to execute custom code.
Here's how emitting a class would look:
<%
var parameters = (string[])data;
var namespaceName = parameters[0];
var className = parameters[1];
%>
namespace <%= namespaceName %>
{
public class <%= className %>
{
}
}
You can of course loop through things:
<% foreach (var parameter in parameters) { %>
<%= parameter %>
<% } %>
and put code in if-blocks etc.
The class library is released on CodePlex here:
as well as on NuGet.
The project comes with examples, download the source or browse it online.
To answer questions by email also here, for others to see:
data
parameter corresponds to whatever was passed in as the parameter to the .Generate(x)
method from your application, and is of the same type. If you pass in an object you have defined in your own class libraries, you need to add a reference to the template code in order to properly access it. (<%@ reference your.class.library.dll %>
).Generate()
. If you don't call .Compile()
yourself, the first call to .Generate()
will take care of it. Also note that the code runs in a separate appdomain, so there's a slight marshalling overhead related to copying the parameter and result back and forth. The code, however, runs at normal JITted .NET code speed.Example of if-block:
<% if (a == b) { %>
This will only be output if a==b.
<% } %>
There's no artificial limits on formatting the code either, pick the style that suits you best:
<%
if (a == b)
{
%>
This will only be output if a==b.
<%
}
%>
Only note that all non-code parts of the template will pretty much be output as-is, which means tabs and such following %>
blocks will be output as well.
There is one limit, all the code you write must fit inside a single method call.
Let me explain.
The way the template engine works is that it produces a .cs file and feeds it to the C# compiler, this .cs file roughyly looks like this:
using directives
namespace SomeNamespace
{
public class SomeClass
{
public string Render(object data)
{
... all your code goes here
}
}
}
This means that you cannot define new classes, new methods, class-level fields, etc.
You can, however, use anonymous delegates to create functions internally. For instance, if you want a uniform way of formatting dates:
Func<DateTime, string> date2str = delegate(DateTime dt)
{
return dt.ToString("G");
};
then you can simply use that in the rest of the template code:
<%= date2str(DateTime.Now) %>
Only requirement I have is that you don't upload the files onto the web and claim you wrote the code, other than that you're free to do what you want with it.
Edit 23.04.2011: Fixed links to CodePlex project.
Upvotes: 16
Reputation: 6606
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/
Upvotes: 14
Reputation: 1216
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.
Upvotes: 6
Reputation: 43698
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.
Upvotes: 3