2008-12-04 87 views
48

我正在尋找一個.net模板引擎 - 一些簡單,輕量,穩定且不需要太多依賴的東西。目前我需要的是創建模板化的純文本和html電子郵件。任何人都可以給我一個好的建議嗎?你能推薦一個.net模板引擎嗎?

如果它有幫助 - 就像Java的FreemarkerVelocity庫。

[更新] 感謝您的答案,迄今爲止 - 非常感謝。當你使用這些庫的時候,我的建議和戰爭故事都是真實的。似乎是最好的方式做出決定,而不是每個輪流嘗試。

回答

21

這裏有一對夫婦:

關於NVelocity,它已被青山傢伙分叉,它是開發here

對於電子郵件,我從來不需要比NVelocity更多。

+1

感謝您的mausch。你有沒有與NVelocity有關的問題? – serg10 2008-12-04 11:36:05

0

你見過Velocity的.NET端口NVelocity嗎? http://nvelocity.sourceforge.net/

+0

是 - 谷歌發現,但它不」看起來非常活躍。自2003年以來什麼都沒有。你用過它了嗎? – serg10 2008-12-04 10:16:37

+0

其分支由Castle在http://www.castleproject.org/castle/projects.html – jhexp 2010-05-25 10:13:30

5

更完整的列表

  • ASP.Net Web窗體內置的視圖引擎
  • ASPView
  • 抄網
  • NHaml(.NET Haml的港)
  • 星火
  • NVelocity
  • StringTemplate.Net
2

我認爲Mustache(http://mustache.github.com/)也可能符合法案。

0

NVELOCITY儘管很舊,但在2003年的最後一次發佈,足夠了。

5

RazorEngine,一個建立在微軟Razor解析引擎上的模板引擎。

https://github.com/Antaris/RazorEngine

還沒有使用它,但我覺得很有意思,因爲如果你有一個ASP.NET MVC的背景,你會不會需要學習新的東西。

0

SharpTAL - 獨立引擎在積極發展,沒有依賴關係,快速

1

DotLiquid是.NET非常漂亮的模板系統。

它來源於Ruby的Liquid Markup,需求.NET Framework 3.5或更高版本。

2

與車把,RazorEngine和SharpTAL以下一些測試:

namespace ConsoleApplication4 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 

     Stopwatch sw = new Stopwatch(); 

     //RAZOR 
     string razorTemplate = @"@model ConsoleApplication4.Test 
           <h1>@Model.Title</h1> 
           @if(Model.Condition1) 
           { 
            <span>condition1 is true</span> 
           } 
           <div> 
            @foreach(var movie in Model.Movies) 
             {<span>@movie</span>} 
           </div>"; 

     //burning 
     Engine.Razor.RunCompile(razorTemplate, "templateKey", typeof(Test), new Test()); 
     sw.Start(); 
     var result1 = Engine.Razor.RunCompile(razorTemplate, "templateKey", typeof(Test), new Test()); 
     sw.Stop(); 
     Console.WriteLine("razor : "+sw.Elapsed); 


     //SHARPTAL 
     string sharpTalTemplate = @"<h1>${Title}</h1>    
            <span tal:condition=""Condition1"">condition1 is true</span>          

             <div tal:repeat='movie Movies'>${movie}</div>"; 


     var test = new Test(); 
     var globals = new Dictionary<string, object> 
     { 
      { "Movies", new List<string> {test.Movies[0],test.Movies[1],test.Movies[2] } }, 
      { "Condition1", test.Condition1 }, 
      { "Title", test.Title }, 
     }; 



     var tt = new Template(sharpTalTemplate); 
     tt.Render(globals); 
     sw.Restart(); 
     var tt2 = new Template(sharpTalTemplate); 
     var result2 = tt2.Render(globals); 
     sw.Stop(); 
     Console.WriteLine("sharptal : " + sw.Elapsed); 



     //HANDLEBARS 
     string handleBarsTemplate = @"<h1>{{Title}}</h1> 
           {{#if Condition1}}          
            <span>condition1 is true</span> 
           {{/if}} 
           <div> 
            {{#each Movies}} 
             <span>{{this}}</span> 
            {{/each}}           
           </div>"; 
     var tt3 = Handlebars.Compile(handleBarsTemplate); 
     sw.Restart(); 
     var result3 = tt3(new Test()); 
     sw.Stop(); 
     Console.WriteLine("handlebars : " + sw.Elapsed); 

     Console.WriteLine("-----------------------------"); 
     Console.WriteLine(result1); 
     Console.WriteLine(result2); 
     Console.WriteLine(result3); 

     Console.ReadLine(); 
    } 
} 

public class Test 
{ 
    public bool Condition1 { get; set; } 
    public List<string> Movies { get; set; } 
    public string Title { get; set; } 

    public Test() 
    { 
     Condition1 = true; 
     Movies = new List<string>() { "Rocky", "The Fifth Element", "Intouchables" }; 
     Title = "Hi stackoverflow! Below you can find good movie list! Have a good day."; 
    } 
} 


} 

和結果:

code results