C# 模板引擎

发布于 2024-08-07 06:56:49 字数 1536 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

泅人 2024-08-14 06:56:50

T4 怎么样,文本模板转换工具包?它应该满足您的要求,并且内置于 Visual Studio 中。

很棒的 T4 资源:

Oleg Sych 的博客

T4 编辑器

T4 工具箱

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

淡墨 2024-08-14 06:56:50

有一篇很好的文章如何使用 RazorView 引擎:
如何使用 RazorEngine 创建可本地化的文本模板引擎

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

走走停停 2024-08-14 06:56:50

SmartFormat 是一个非常简单的库,可以满足您的所有要求。它专注于编写“自然语言”文本,非常适合从列表生成数据或应用条件逻辑。

语法与String.Format极其相似,并且非常简单且易于学习和使用。以下是文档中的语法示例:

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

该库是开源的并且易于扩展,因此您还可以使用其他功能来增强它。

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.

爱的那么颓废 2024-08-14 06:56:50

您看过 XSLT 吗?您必须从 XML 中的源数据格式开始,也许可以通过 xml 序列化您的数据对象。您可以执行循环if 语句 轻松!

Kathleen Dollard 有一本关于通过 XSLT 生成代码的书。

就我个人而言,我是 T4 的忠实粉丝(特别是在生成 C# 时),但您可能会发现由于 XML 和 HTML 是您的输出类型,XSLT 已经涵盖了您。另外,它非常跨平台。

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.

╄→承喏 2024-08-14 06:56:50

我的类库中内置了一个模板引擎,其外观和工作方式与旧式 ASP 或 T4 类似。

您基本上在 <% %> 中编写 C# 代码块,因此可以完成 C# 可以做的大部分事情,但限制是整个模板文件被编译为单个方法。换句话说,您不能在模板内定义辅助类等,但对于辅助方法,您可以使用匿名方法。

示例:

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

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

然后,这将被编译为另一个应用程序域中的 C# 类,并且可以执行以返回包含生成的文本的字符串。

您还可以将参数传递到模板中,还可以引用类库,这意味着您可以传递自定义数据结构,或从模板访问数据访问层或业务逻辑代码。

如果您想查看它,可以从我的 Subversion 存储库或网页中的类库中找到该代码:

对于 subversion 存储库,您需要用户名和密码,两者都是“guest”,不带引号。

代码位于 LVK.Text.Templated 项目/程序集中。

另外,请告诉我(请参阅个人资料页面上的电子邮件,或留下评论),我将为您提供更多示例。

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.

你如我软肋 2024-08-14 06:56:50

您可能需要这个.NET 模板引擎

模板代码:

$Book.StaticId$

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

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

C# 代码:

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);
       }
   }

输出为:

AABB

ID: 100 - Author: John Borders

12

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文