Search code examples
.netrazorrazorengine

RazorEngine vs RazorTemplates vs RazorMachine


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.


Solution

  • 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