C#模板引擎
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1518954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# template engine
提问by
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.
我正在寻找一个独立的、易于使用的 C# 代码模板引擎。我想创建一个带有数据占位符的 HTML 和 XML 文件,并用我的代码中的数据填充它们。
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?
引擎需要支持循环(复制模板的一部分形成多个对象)和条件(仅当某些条件为真时才将模板的一部分添加到最终的 HTML/XML)。有人可以为我推荐一个好的选择,并添加一个或多或少的此类代码示例的链接,以及一些关于如何使用推荐组件满足我的需求的文档吗?
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).
我还需要使用循环来复制表格行,甚至整个表格(在 HTML 版本中)和复杂元素(在 XML 版本中)。
回答by Fredrik M?rk
I have used StringTemplatewith good results. Some resources:
我使用StringTemplate效果很好。一些资源:
回答by Paolo Tedesco
What about T4, Text Template Transformation Toolkit? It should fit your requirements, and is built-in in Visual Studio.
T4,文本模板转换工具包怎么样?它应该符合您的要求,并且内置在 Visual Studio 中。
Great T4 resources:
很棒的T4资源:
回答by 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 loopsand if statementswith ease!
你看过XSLT吗?您必须从 XML 中的源数据格式开始,可能是通过 xmlserializing 您的数据对象。您可以轻松执行循环和if 语句!
Kathleen Dollard has a book on generating code via XSLT.
Kathleen Dollard 有一本关于通过 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.
就我个人而言,我是T4 的忠实粉丝(尤其是在生成 C# 时),但您可能会发现,由于 XML 和 HTML 是 XSLT 涵盖的输出类型。此外,它是非常跨平台的。
回答by Lasse V. Karlsen
I have a templating engine built into my class library that looks and works similar to old-style ASP, or T4 for that matter.
我的类库中内置了一个模板引擎,它的外观和工作方式与旧式 ASP 或 T4 类似。
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.
您基本上可以在 <% %> 块中编写 C# 代码,因此可以做 C# 可以做的大多数事情,但整个模板文件被编译为单个方法。换句话说,您不能在模板中定义辅助类等,但对于辅助方法,您可以使用匿名方法。
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.
然后这将被编译为另一个应用程序域中的 C# 类,并且可以执行以返回包含生成的文本的字符串。
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:
如果您想查看它,可以在我的 Subversion 存储库或网页中的类库中找到代码:
- Subversion repository of source code
- Subversion repository of binaries, contains latest checked in source code that builds and passes unit tests)
- 源代码的Subversion存储库
- 二进制文件的 Subversion 存储库,包含构建和通过单元测试的最新签入的源代码)
For the subversion repositories you need a username and password, both are "guest", without the quotes.
对于 subversion 存储库,您需要用户名和密码,两者都是“guest”,没有引号。
The code is in the LVK.Text.Templating project/assembly.
代码位于 LVK.Text.Templating 项目/程序集。
Also, let me know (see email on profile page, or leave comment) and I'll provide you with some more examples.
另外,请告诉我(请参阅个人资料页面上的电子邮件,或发表评论),我将为您提供更多示例。
回答by Lasse V. Karlsen
You may need this .NET Template Engine.
您可能需要这个.NET 模板引擎。
Template Code:
模板代码:
$Book.StaticId$
ID: $bk.BookId$ - Author: $bk.Author.Name$
Length of the author's Name: $bk.Author.Name.Length$
C# Code:
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);
}
}
Output is:
输出是:
AABB
ID: 100 - Author: John Borders
12
回答by Scott Rippey
SmartFormatis 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.
SmartFormat是一个非常简单的库,可以满足您的所有要求。它专注于编写“自然语言”文本,非常适合从列表生成数据或应用条件逻辑。
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:
语法与 极其相似String.Format
,非常简单易学易用。以下是文档中的语法示例:
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.
该库是开源的且易于扩展,因此您还可以使用其他功能对其进行增强。
回答by Malkov
There is a nice article how to use RazorView engine: How to create a localizable text template engine using RazorEngine
有一篇很好的文章如何使用 RazorView 引擎: How to create a localizable text template engine using RazorEngine