C# template engine

C#Template Engine

C# Problem Overview


I am looking for a stand-alone, easy to use from C# code, template engine. I want to 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?

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).

C# Solutions


Solution 1 - C#

Solution 2 - C#

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

Solution 3 - C#

There is a nice article how to use RazorView engine: How to create a localizable text template engine using RazorEngine

Solution 4 - C#

SmartFormat is a pretty simple library that meets all your requirements. It is focused on composing "natural language" text, and is great for generating data from lists, or applying conditional logic.

The syntax is extremely similar to String.Format, and is very simple and easy to learn and use. Here's an example of the syntax from the documentation:

Smart.Format("{Name}'s friends: {Friends:{Name}|, |, and}", user)
// Result: "Scott's friends: Michael, Jim, Pam, and Dwight"

The library is open source and easily extensible, so you can also enhance it with additional features.

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmeView Question on Stackoverflow
Solution 1 - C#Fredrik MörkView Answer on Stackoverflow
Solution 2 - C#Paolo TedescoView Answer on Stackoverflow
Solution 3 - C#MalkovView Answer on Stackoverflow
Solution 4 - C#Scott RippeyView Answer on Stackoverflow
Solution 5 - C#Rob Fonseca-EnsorView Answer on Stackoverflow
Solution 6 - C#Lasse V. KarlsenView Answer on Stackoverflow
Solution 7 - C#William RichardsView Answer on Stackoverflow