views:

1575

answers:

6
+4  Q: 

C# template engine

Hi!

I am looking for a stand-alone, easy to use from C# code, template engine. What I want to do is create an html and xml files with placeholders for data, and fill them with data from my code. The engine needs to support loops (duplicating parts of the template form more that one object) and conditions (add parts of the template to the final html/xml only if some conditions are true). Can someone recommend a good option for me, and add a link to more-or-less such code sample, and some documentation about how to use the recommended component for my needs?

Thanks:)

Just wanted to add one more thing - I also need to use loops to duplicate table rows, or even entire tables (in the html version) and complex elements (in the xml version) Thanks again:)

+3  A: 

I have used StringTemplate with good results. Some resources:

Fredrik Mörk
+6  A: 

What about T4, Text Template Transformation Toolkit? It should fit your requirements, and is built-in in Visual Studio.

Great T4 resources:

Oleg Sych's blog

T4 Editor

T4 Toolbox

Paolo Tedesco
+4  A: 

Have you looked at XSLT? You'll have to start with your source data format in xml, maybe by xmlserializing your data objects. You can do loops and if statements with ease!

Kathleen Dollard has a book on generating code via XSLT.

Personally, I'm a big fan of T4 (especially when generating C#), but you might find that since XML and HTML are your output types XSLT has you covered. Plus it's very cross-platform.

Rob Fonseca-Ensor
+1  A: 

I have a templating engine built into my class library that looks and works similar to old-style ASP, or T4 for that matter.

You basically write C# code in <% %> blocks, and can thus do most things C# can do, with the limitation that the entire template file is being compiled to a single method. In other words, you can't define helper classes and such inside the template, but for helper methods you can do anonymous methods.

Example:

<%
    var firstname = "Bob";
    var count = 10;

    for (Int32 index = 0; index < count; index++)
    {
%>
<%= firstname %> x <%= index+1 %>/<%= count %>
<%
    }
%>

This will then be compiled to a C# class in another appdomain, and can be executed to return the string containing the produced text.

You can also pass an argument into the template, and also reference class libraries, which means you can pass custom data structures, or access data access layer or business logic code from your template.

If you want to look at it, the code is available in my class library from my Subversion repository or web page:

For the subversion repositories you need a username and password, both are "guest", without the quotes.

The code is in the LVK.Text.Templating project/assembly.

Also, let me know (see email on profile page, or leave comment) and I'll provide you with some more examples.

Lasse V. Karlsen
I don't quite understand why you have re-invented ASP.NET ...
Noon Silk
Considering I'm using it to generate code inside Visual Studio, amongst other things, I don't think it would be feasible to set up a full ASP.NET engine locally for that. I think you would balk at StringTemplate, as referred to by others to have a requirement that you would need a full IIS configuration set up for this.
Lasse V. Karlsen
@silky ASP.NET isn't just a templating language.
chakrit
I wish you hosted this on something more lasting, as I get 404's all around :) I would personally recommend BitBucket. In addition to providing free hosting that lasts, it also makes it that much easier for others to contribute tweaks by using Mercurial (SVN's design principles are a bit too ancient...)
romkyns
I am working on it, replacing my references with something that lasts, but I will find a place which I control where I can script an upload, otherwise I'll have to release full snapshots of my source code, and it'll be a nightmare to maintain, but I agree, I need to find something better than the current.
Lasse V. Karlsen
Fixed the reference in a few posts, I'll see if I can find a way to go through them all, the repository is still up, just a slightly different url. As for "contributing tweaks by using Mercurial", I'm not going to change source control tool, and I'm not going to place my source on a public repository, so that's not going to happen. Subversion is perfect for my needs.
Lasse V. Karlsen
A: 

I wrote one

http://www.csharpages.com

TheQult
+1  A: 

You may need this .NET Template Engine.

Template Code:

$Book.StaticId$

ID: $bk.BookId$ - Author: $bk.Author.Name$

Length of the author's Name: $bk.Author.Name.Length$

C# Code:

class Author
   {
       public string Name
       {
           get
           {
               return "John Borders";
           }
       }
   }
   class Book
   {
       public static string StaticId
       {
           get
           {
               return "AABB";
           }
       }
       public int BookId
       {
           get
           {
               return 100;
           }
       }
       public Author Author
       {
           get
           {
               return new Author();
           }
       }
   }
   public class PropertySample1
   {
       [STAThread]
       static void Main()
       {
           TemplateEngine dt = new TemplateEngine();
           dt.LoadFromFile("Template.tpl");
           Book book = new Book();
           dt.SetValue("bk", book);
           dt.UsingNamespace("CSharp,Demo");
           string output = dt.Run();
           Console.WriteLine(output);
       }
   }

Output is:

AABB

ID: 100 - Author: John Borders

12
I can't imagine why anyone would pay money for that as opposed to just using T4 templates. Are these better than T4 templates in some way?
John Saunders