可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm looking for a template engine for .NET/C# to generate email notifications in my application. I read about NVelocity in the past and think it would fit my needs, but it seems this project is dead.
Would you still recommended to use NVelocity for that purpose or can you suggest any alternatives?
Note: I found some other templating engines, but these are mostly "view-engines" for ASP.NET MVC (Brail, NHaml, etc.). But I think these are not what I'm looking for.
回答1:
The Castle Project forked the project and have been maintaining it, and most recently released version 1.1.1 on 10 December 2010. More details can be found at the Castle Projects NVelocity site. They have also made a number of improvements to it, details of which can be found on their NVelocity improvements page. The project looks like it is now dead and in MonoRail 3, it has been replaced by Castle Blade which is based off Razor.
If you are looking for an alternative, then I would suggest the Razor view engine from ASP.Net MVC3 or the Spark View Engine.
The open source project RazorEngine allows you to use the the Razor View engine outside of ASP.Net MVC or Web Pages so you can use it to generate templated emails.
回答2:
You could use brail or nhaml standalone to generate html emails.
The core of nhaml (not sure about brail??) has no references to MVC or ASP.
I have used nhaml to generate XPS documents for printing and reporting from a win forms app.
If it has to be text perhaps http://www.stringtemplate.org/
And here is a similar question How to use Brail as a stand-alone general purpose templating engine (like NVelocity)?
回答3:
I don't see a reason not to use NVelocity (the Castle project fork). It's stable and your specific needs (email templates) are probably not as complex as the ones from other projects which use NVelocity, so you shouldn't expect any problems.
回答4:
There is project on codeplex.
http://www.codeplex.com/NVelocity
Project Description:
NVelocity is a port of very propular open source java template enginee
framework,velocity 1.6.
NVelocity is a pure .Net library written in C# which currently targets
Framework version 3.5.
The initial source code checked-in appears to be straight from
svn.castleproject.org.
The person who create the CodePlex project (username: TerryLiang) also
created one back in August from nLucene. For that project, he has
made about 50 checkins since then, so perhaps he's planning to do some
serious work on that project.
回答5:
There's nothing wrong with NVelocity except that it's hard to find the most updated version of the project. Google typically turns up the old sourceforge link as the first hit, and the codeplex link is the next most common. The place to get it is from Castle as Pervez Choudhury mentioned. We use the Castle version of NVelocity at work, and it's great.
There's another option that emerged in the years since this question was first asked, and that is Microsoft's T4. Most people think of T4 as a pre-compilation generator as that was how it was first release, but actually you can now use it to generate a generator class and do dynamic generation at runtime. It's called a preprocessed T4 template, and you can read more about it here. The advantage is that it's already built into Visual Studio so you don't need to do anything special to get going.
回答6:
As NVelocity seems to have died (again), I would suggest DotLiquid. Cannot say much for now as I'm still evaluating it, but it sure looks promising (and builds as MonoTouch/MonoDroid library for what I'm concerned).
UPDATE [11 Jun 2012]
The constraint of having "safe" classes/collections as models may be a major source of headaches. When evaluating this engine, you may want to start from this point.
回答7:
I agree with Simon; http://www.stringtemplate.org/ is excellent, very well designed and is being actively maintained.
回答8:
I can't upvote Sorin for DotLiquid, so just want to add my comment - it seems like a great platform being actively developed.
And with features they have in trunk version (not in the released dll tho) you can use pocos without the need to inherit from any base class if you register them this way:
foreach (var type in {Assembly}.GetTypes())
Template.RegisterSafeType(type, type.GetProperties().Select(p => p.Name).ToArray());
and this solved the headaches Sorin mentioned.
StringTemplate is also a great thing, but too strict and limiting, templates are too pure Views and it is inpractical to use...
回答9:
You can use the original (and frequently updated) Java version of Velocity in .Net by using IKVM to convert it to an assembly. I have used velocity successfully this way for code generation under .Net. You might also take a look at StringTemplate. There are java and C# versions of StringTemplate. You can find a quick mini-tutorial on using Velocity in .Net here. I suggest taking a hard look at StringTemplate however, as I think it is a cleaner template engine (I'm a recent convert).
回答10:
I would recommend using XSLT for templating. Yes, it requires a little more time to learn the technology, but then you have much more flexible template solution than anything else.