2008-12-06 83 views
2

我正在寻找免费的模板生成引擎来生成简单的报告。我想一些基本的功能,如:用于在.NET中生成简单报告的模板引擎

  • 笔头循环(与任何的IEnumerable)
  • 传递变量
  • 传递模板文件(主模板,页脚,标题)

我我们将使用它来生成HTML和XML报告。我不在寻找一个ASP.NET模板引擎。

这是一个WinForms应用程序。

我见过这个问题Can you recommend a .net template engine?,但是所有这些模板引擎对我来说都是过度杀毒,并且专注于ASP.NET。

请只推荐免费的图书馆。

//我仍在寻找一个NVelocity,但它看起来没有什么前途的.NET,过于复杂,当你下载它的一堆文件不清楚该怎么做,没有教程,启动文档等

+0

我不知道答案,但提示:如果您想吸引更多用户,请删除问题并重新插入,并且不要设置社区wiki标志。 “社区维基”upvotes不增加用户的声誉... – splattne 2008-12-06 16:49:26

+0

感谢您的提示,我不知道这一限制。我会为未来的问题做到这一点。 – 2008-12-06 17:05:14

回答

3

我第二次不推荐nVelocity。这是一个可怕的港口。

.NET实际上使用CodeDOM构建了模板功能。

这里是如何做到这一点相当不错演练:

http://www.codeproject.com/KB/cs/smarttemplateengine.aspx

有了一些小的编码,你就可以创建具有内嵌C#模板:

<html> 
    <head> 
     <title>My Report</title> 
    </head> 
    <body> 
     <% foreach (ReportRow r in ReportData) { %> 
      <!-- Markup and Code for Report --> 
     <% } %> 
    </body> 
</html> 
1

我会推荐StringTemplate(http://www.stringtemplate.org)。我用它来生成邮件。您可以使用存储在Filesystem或数据库中的模板组(花费一点点工作)。

+0

你是否知道一些.NET模板的.NET例子的网站? – 2008-12-06 16:56:56

2

我们使用NVelocity作为我们基于MonoRail的网站的一部分。坦率地说,我不会推荐NVelocity。它是一个直接的端口(并且直接指CTRL + C,CTRL + V,将一些Java关键字更改为C#关键字 - 这是不可维护的)从Java版本转换为.NET版本。由于原来的.NET维护者将其置于不健康的状态,因此Castle不得不分发它来添加一些面向.NET的功能(字典访问,花哨的foreach循环)。如果你尝试在多行中分割一个long if语句,有些问题会转义掉它自己的一些指令。我不是故意要抛弃它,它是一种很好的语言,它是免费的,它用在我们的网站上,它每天处理数千个请求 - 我只是不会再使用它,除非我看到一个新的实现相同的语法。

对于生成电子邮件的系统的其他领域,我只是使用旧式方法来替换字符串中的$ SPECIAL_KEYWORDS $(即,将关键字映射为值的字典,遍历它并替换) 。效率不高,但效果不错。

期待阅读一些其他答案。

0

感谢您的帮助,CodeDOM专门给我展示了一种不错的方式, 现在我正在使用这个:http://www.stefansarstedt.com/templatemaschine.html

这不是最好的,有点脏,但为我做的伎俩。这是适合我的LGPL,简称。

我看了看其他的建议,但不喜欢他们,太复杂了我想要的东西(文件和DLL相当混乱-tons,依赖性等 - )

0

约翰Resig的有一个伟大的Javascript模板结尾叫做micro-template。为模板的语法是:

<script type="text/html" id="item_tmpl"> 
    <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>"> 
    <div class="grid_1 alpha right"> 
     <img class="righted" src="<%=profile_image_url%>"/> 
    </div> 
    <div class="grid_6 omega contents"> 
     <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p> 
    </div> 
    </div> 
</script> 

模板功能将支持使用JavaScript,因此你得到的循环功能,并完全访问DOM,所以任何的jQuery或其他框架为您处理。这是一种非常灵活的方法,因为它可以让您以很少的努力和改动快速将新功能包含到网页中。它也适用于.Net,如Dave Ward的post所示。

该模板可以是一个html文件,您可以使用$ .get()命令检索该文件。您为模板提供一个JSON对象作为数据源。最后,还有一个功能允许您预编译模板。

0

这里是另一个模板引擎:UltTemplate Engine

这里是模板代码:

Dear $User.FullName$, 
{%set orders=User.GetOrders() /} 
Thank you for your order of $orders.Length$ items, We believe you will be very satisfied with the quality of costume pieces included in each. It is this quality that makes our imaginative play apparel so unique. 

We created an account for you to track your orders. Here is the login information: 
Email: $User.EmailAddress$ 
Password: $User.Password$ 

Following is the details of your order (OrderId: $OrderId$): 
# Part ID Name   Quantity  Price(per unit)  Sub Total 
{%set Total=0.0 /}{%foreach orderproduct,i in orders%}{%set Total = Total + orderproduct.Price * orderproduct.Quantity/} 
{%rendertemplate orderproducttemplate item=orderproduct/}{$foreach%} 
                           Total: $PadLeft(Format(Total,"$$#.##"),11)$ 

If you have any concern, please call us at 913-555-0115. 

Sincerely, 

$CompanyName$ 

{%template orderproducttemplate%}$PadLeft(i,4)$$PadLeft(item.PartId, 7)$ $PadRight(item.ProductName, 15)$  $PadRight(item.Quantity, 5)$  $PadLeft(Format(item.Price,"$$#.##"), 7)$ $PadLeft(Format(item.Price*item.Quantity,"$$#.##"), 12)${$template%} 

,这里是输出:

Dear John Borders, 

Thank you for your order of 3 items, We believe you will be very satisfied with the quality of costume pieces included in each. It is this quality that makes our imaginative play apparel so unique. 

We created an account for you to track your orders. Here is the login information: 
Email: [email protected] 
Password: 123abc 

Following is the details of your order (OrderId: 1625DGHJ): 
# Part ID Name   Quantity  Price(per unit)  Sub Total 

    0 1239 Product A    3   $104.09  $312.27 
    1  22 Product B    1   $134.09  $134.09 
    2 167 Product C    5    $14.7   $73.5 

               Total:  $519.86 

If you have any concern, please call us at 913-555-0115. 

Sincerely, 

My Company Name 

下面是C#代码:

 class OrderProduct 
     { 
      private int _partId; 
      private string _productName; 
      private int _quantity; 
      private float _price; 

      public int PartId 
      { 
       get { return _partId; } 
       set { _partId = value; } 
      } 

      public string ProductName 
      { 
       get { return _productName; } 
       set { _productName = value; } 
      } 

      public int Quantity 
      { 
       get { return _quantity; } 
       set { _quantity = value; } 
      } 

      public float Price 
      { 
       get { return _price; } 
       set { _price = value; } 
      } 
     } 

     class User 
     { 
      private string _fullName; 
      private string _emailAddress; 
      private string _password; 

      public string FullName 
      { 
       get { return _fullName; } 
       set { _fullName = value; } 
      } 

      public string EmailAddress 
      { 
       get { return _emailAddress; } 
       set { _emailAddress = value; } 
      } 

      public string Password 
      { 
       get { return _password; } 
       set { _password = value; } 
      } 

      public OrderProduct[] GetOrders() 
      { 
       OrderProduct[] ops = new OrderProduct[3]; 

       ops[0] = new OrderProduct(); 
       ops[0].PartId = 1239; 
       ops[0].Price = 104.09f; 
       ops[0].ProductName = "Product A"; 
       ops[0].Quantity = 3; 

       ops[1] = new OrderProduct(); 
       ops[1].PartId = 22; 
       ops[1].Price = 134.09f; 
       ops[1].ProductName = "Product B"; 
       ops[1].Quantity = 1; 

       ops[2] = new OrderProduct(); 
       ops[2].PartId = 167; 
       ops[2].Price = 14.7f; 
       ops[2].ProductName = "Product C"; 
       ops[2].Quantity = 5; 

       return ops; 
      } 
     } 

     private void btnRun_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       dt.LoadFromString(txtSource.Text); 
       dt.SetValue("CompanyName", "My Company Name"); 

       User u = new User(); 
       u.EmailAddress = "[email protected]"; 
       u.FullName = "John Borders"; 
       u.Password = "123abc"; 
       dt.SetValue("User", u); 
       dt.SetValue("OrderId", "1625DGHJ"); 

       txtOutput.Text = dt.Run(); 
      } 
      catch (Exception exc) 
      { 
       MessageBox.Show("An error occurred: " + exc.Message); 
      } 
     } 
2

虽然我看到你已经接受了答案,但我鼓励你看看Spark view engine。从网站

例子:

<viewdata products="IEnumerable[[Product]]"/> 
<ul if="products.Any()"> 
    <li each="var p in products">${p.Name}</li> 
</ul> 
<else> 
    <p>No products available</p> 
</else> 
0

,因为我通过这些答案,我不禁注意到,所有的答案,图书馆等都是较小的应用程序相当复杂的阅读。因此,我想建议T4T Text Templates

这里是一个教程Writing之一。它不需要任何未包含在Visual Studio中的库。如果你不需要过于复杂的模板或报告,可能值得一窥。

模板的代码示例:

<#@ template language="C#" #> 
<html><body> 
<h1>Sales for Previous Month</h2> 
<table> 
    <# for (int i = 1; i <= 10; i++) 
     { #> 
     <tr><td>Test name <#= i #> </td> 
      <td>Test value <#= i * i #> </td> </tr> 
    <# } #> 
</table> 
This report is Company Confidential. 
</body></html> 

在应用程序代码,您可以使用这样的调用生成模板的内容:

MyWebPage page = new MyWebPage(); 
String pageContent = page.TransformText(); 
System.IO.File.WriteAllText("outputPage.html", pageContent);