我正在寻找一个 .net 模板引擎——简单、轻量、稳定且没有太多依赖项的东西。我现在需要的只是创建模板化的纯文本和 html 电子邮件。谁能给我一个好的建议?
如果它有帮助的话——比如 Java 的Freemarker或Velocity库。
[更新] 感谢您到目前为止的答案 - 非常感谢。当您使用这些库时,我对推荐或战争故事非常感兴趣。似乎是做出决定而不是依次尝试的最佳方式。
我正在寻找一个 .net 模板引擎——简单、轻量、稳定且没有太多依赖项的东西。我现在需要的只是创建模板化的纯文本和 html 电子邮件。谁能给我一个好的建议?
如果它有帮助的话——比如 Java 的Freemarker或Velocity库。
[更新] 感谢您到目前为止的答案 - 非常感谢。当您使用这些库时,我对推荐或战争故事非常感兴趣。似乎是做出决定而不是依次尝试的最佳方式。
RazorEngine,一个基于微软 Razor 解析引擎的模板引擎。
https://github.com/Antaris/RazorEngine
没用过,但我觉得它很有趣,因为如果你有 ASP.NET MVC 背景,你不需要学习新东西。
更完整的清单
我会推荐CodeSmith Generator。它是一个基于模板的代码生成器,具有不断更新和活跃的社区。下面是CodeSmith Generator 附带的模板列表。
Handlebars、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.";
}
}
}
和结果:
我刚刚发布了一个开源项目。它主要针对电子邮件模板,但如果您愿意,可以单独使用解析器。您可以阅读更多详细信息并从我的博客中获取源代码。
我认为 Mustache (http://mustache.github.com/) 也可能符合要求。
DotLiquid 是非常好的 .NET 模板系统。
它源自 Ruby 的 Liquid Markup,要求 .NET Framework 3.5 或更高版本。
试试这个:电子邮件模板框架http://www.bitethebullet.co.uk/Email_Template_Framework.aspx
它在 ASP.NET 和 WinForms 下运行良好,并且仍在积极开发中。还有非常好的文档,并且很容易在示例中挖掘。
<ul>
<c:for-each name='n' in='System.Linq.Enumerable.Range(1, 5)' expand-text='yes'>
<li>{n}</li>
</c:for-each>
</ul>
你见过 NVelocity,一个 Velocity 的 .NET 端口吗? http://nvelocity.sourceforge.net/
http://csharp-source.net/open-source/template-engines
http://joel.net/code/dotnet_templates.aspx
希望这可以帮助!!!
NVELOCITY,虽然它很旧,但在 2003 年最后一次发布,足够了。
SharpTAL - 活跃开发中的独立引擎,没有依赖,快速