Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 11 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
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
有一篇很好的文章如何使用 RazorView 引擎:
如何使用 RazorEngine 创建可本地化的文本模板引擎
There is a nice article how to use RazorView engine:
How to create a localizable text template engine using RazorEngine
SmartFormat 是一个非常简单的库,可以满足您的所有要求。它专注于编写“自然语言”文本,非常适合从列表生成数据或应用条件逻辑。
语法与
String.Format
极其相似,并且非常简单且易于学习和使用。以下是文档中的语法示例:该库是开源的并且易于扩展,因此您还可以使用其他功能来增强它。
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:The library is open source and easily extensible, so you can also enhance it with additional features.
您看过 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.
我的类库中内置了一个模板引擎,其外观和工作方式与旧式 ASP 或 T4 类似。
您基本上在 <% %> 中编写 C# 代码块,因此可以完成 C# 可以做的大部分事情,但限制是整个模板文件被编译为单个方法。换句话说,您不能在模板内定义辅助类等,但对于辅助方法,您可以使用匿名方法。
示例:
然后,这将被编译为另一个应用程序域中的 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:
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.
您可能需要这个.NET 模板引擎。
模板代码:
C# 代码:
输出为:
You may need this .NET Template Engine.
Template Code:
C# Code:
Output is:
我使用 StringTemplate 取得了良好的效果。一些资源:
I have used StringTemplate with good results. Some resources: