52

I am looking for a .net templating engine - something simple, lightweight, stable with not too many dependencies. All I need it for at the moment is creating templated plain text and html emails. Can anyone give me a good recommendation?

If it helps at all - something like Java's Freemarker or Velocity libraries.

[UPDATE] Thanks for the answers so far - much appreciated. I am really intested in recommendations or war stories from when you have used these libraries. Seems to be the best way to make a decision without trying each in turn.

Jon Limjap
  • 89,838
  • 14
  • 96
  • 150
serg10
  • 28,946
  • 16
  • 65
  • 92

15 Answers15

21

Here's a couple more:

About NVelocity, it has been forked by the Castle guys, it's being developed here

For emails, I've never needed more than NVelocity.

Mauricio Scheffer
  • 96,120
  • 20
  • 187
  • 273
6

RazorEngine, A templating engine built on Microsoft's Razor parsing engine.

https://github.com/Antaris/RazorEngine

Haven't used it, but I find it interesting because if you have an ASP.NET MVC background, you won't need to learn something new.

CGK
  • 2,462
  • 19
  • 24
  • It was some dependencies, and I have many trouble because of using older version of system.web.razor and razorEngine need newer, so in local I didn't have trouble because of GAC assemblies, but on the remote server :( finally I preferred to use another template engine rather wasting time with testing version conflict and etc. – QMaster Mar 02 '17 at 12:07
5

I would recommend CodeSmith Generator. It is a template based code generator, with constant updates and an active community. Here is a list of templates that ship with CodeSmith Generator.

Blake Niemyjski
  • 3,192
  • 3
  • 23
  • 40
KellyCoinGuy
  • 51
  • 1
  • 3
5

More Complete List

  • ASP.Net inbuilt WebForm View Engine
  • ASPView
  • Brail
  • NHaml (.Net port of Haml)
  • Spark
  • NVelocity
  • StringTemplate.Net
Palani
  • 8,532
  • 10
  • 51
  • 62
4

string template from the anltr.org folks with a C# version too.

kenny
  • 18,952
  • 7
  • 47
  • 82
3

Some tests with Handlebars, RazorEngine and SharpTAL below :

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.";
    }
}


}

and results :

code results

Nigrimmist
  • 5,339
  • 3
  • 36
  • 39
2

I think Mustache (http://mustache.github.com/) may fit the bill too.

mp31415
  • 5,743
  • 1
  • 40
  • 32
2

DotLiquid is very nice templating system for .NET.

It's derived from Ruby’s Liquid Markup, with requirements .NET Framework 3.5 or above.

dns
  • 2,564
  • 1
  • 22
  • 30
2

I've just released an open source project. It's aimed principally at email templating but you could use the parser by itself if you wanted to. You can read some more details and grab the source code from my blog.

http://thecodedecanter.wordpress.com/2010/07/19/town-crier-an-open-source-e-mail-templating-engine-for-net/

Oenotria
  • 1,582
  • 9
  • 22
1

XCST (eXtensible C-Sharp Templates)

<ul>
   <c:for-each name='n' in='System.Linq.Enumerable.Range(1, 5)' expand-text='yes'>
      <li>{n}</li>
   </c:for-each>
</ul>
Max Toro
  • 27,264
  • 10
  • 71
  • 109
1

try this one: Email Template Framework http://www.bitethebullet.co.uk/Email_Template_Framework.aspx

It works great under ASP.NET and WinForms and is still under active development. There is also very nice documentation and easy to dig in examples.

binball
  • 2,077
  • 4
  • 26
  • 34
0

NVELOCITY, though it's old ,last release in 2003, enough.

Tredency
  • 21
  • 5
0

SharpTAL - standalone engine in active development and without dependencies, fast

rlacko
  • 561
  • 3
  • 11
0

Have you seen NVelocity, a .NET port of Velocity? http://nvelocity.sourceforge.net/

MattValerio
  • 261
  • 1
  • 2
  • 7
0

http://csharp-source.net/open-source/template-engines

http://joel.net/code/dotnet_templates.aspx

Hope this helps!!!

Samiksha
  • 5,852
  • 6
  • 26
  • 28