C# template engine [closed]

落花浮王杯 提交于 2019-11-27 17:22:50
Fredrik Mörk

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

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

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.

Rob Fonseca-Ensor

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.

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!