RazorEngine vs RazorTemplates vs RazorMachine [closed]
Asked Answered
R

3

21

Can someone explain what are the differences, pros/cons between

RazorEngine

RazorTemplates

RazorMachine

I need to pick one for email generation. The requirements are quite usual: fast, ease-of-use. It seems like all of them has all features I need but as I'm Razor newbie it's not quite clear to me which one is better.

Thanks.

Ryannryazan answered 15/8, 2013 at 13:34 Comment(0)
C
19

I have tried all 3 libraries myself and found several differences.

  • RazorEngine - the easiest one and works best among all of them, supports caching by default.
  • RazorTemplates - doesn't support some Razor directives (for example @model) and uses lots of dynamic types. Can use precompiled templates.
  • RazorMachine - very unpredictable and with a strange design that requires to use a global single instance instead of a static class.

As to me, I have chosen RazorEngine. Also here is the code how to use these libraries:

RazorEngine

string html = Razor.Parse(templateContent, model, templatePath);

RazorTemplates

if (!_templatesCache.ContainsKey(templatePath))
{
    var compiledTemplate = Template.Compile(templateContent);
    _templatesCache.Add(templatePath, compiledTemplate);
}

string html = _templatesCache[templatePath].Render(model);

RazorMachine

private readonly Lazy<RazorMachine> _lazyRazorMachine = 
    new Lazy<RazorMachine>(() => new RazorMachine());
//...

var rm = _lazyRazorMachine.Value;
string html = rm.ExecuteContent(templateContent, model, null, true).Result;

And some performance tests, tested each library 2 times on the same template, all of them have a similar performance without a big difference:

RazorEngine - 1731 ms, 0.1 ms

RazorTemplates - 1753 ms, 0.1 ms

RazorMachine - 1608 ms, 0.1 ms

Collimate answered 30/10, 2013 at 11:23 Comment(5)
Did you use the same instance of the RazorMachine object across multiple tests? That's supposed to be an app level singleton that manages all of the caching and precompiling: github.com/jlamfers/RazorMachine/wiki/ExamplesChamplain
@Chris Hynes Yes, it turned out the issue was because I created a new instance each time, now I fixed the code. Though the fact remains, library works in a very unexpected way.Collimate
How does RazorEngine work with advanced stuff like models, custom view classes, layouts, viewstarts, etc? RazorMachine, convoluted as it is, it the only thing I've found that actually works with the full gamut of Razor features. I put together a couple of classes that work with RazorMachine to provide a full Razor like environment with layouts, viewstart, etc., but running based on embedded resource files. This lets me use the same templates from a web project, service project, or what have you.Champlain
@Chris Hynes Other libraries are not supposed to replicate the structure of asp.net mvc projects with configuration files, _viewstart and _layout files. They are rather used in separate class libraries for simple email or page generations. Though they support partial includes, models and custom methods, but they don't support the whole asp.net view infrastructure.Collimate
@vorrtex Which of them can be debugged in VS 2015 in a non-asp.net project? That would be a cool answer!Frentz
U
0

I use RazorEngine for email generation and it works just fine. Looking at the other 2 projects it appears they do the same like RazorEngine - hosting MS Razor. Anyway, can recommend the latter for your purpose.

Upthrow answered 15/8, 2013 at 13:48 Comment(0)
R
-1

Use, https://github.com/smsohan/MvcMailer it really helps with making MVC Razor based emails.

Rubicund answered 15/8, 2013 at 13:50 Comment(1)
But only if you send you emails from your Asp.net MVC application. It's unfortunately not possible to use MvcMailer from within a class library. And in an enterprise application, emails are usually sent from the business layer... But it's a nice library though... ;)Branchiopod

© 2022 - 2024 — McMap. All rights reserved.