Can you recommend a .net template engine? [closed]
Asked Answered
O

15

56

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.

Omnidirectional answered 4/12, 2008 at 10:6 Comment(0)
K
23

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.

Kelvin answered 4/12, 2008 at 11:15 Comment(7)
Thanks for that mausch. Have you had any issues with NVelocity?Omnidirectional
Yes, there are some minor issues, for example this one: forum.castleproject.org/viewtopic.php?t=4781Kelvin
All links are pretty much dead...Bushman
@Bushman only one actually, and IMHO that's pretty decent for an answer that's seven years old.Kelvin
Maintenance is also important. There is already a wayback machine :)Bushman
@Bushman I'm not maintaining a 7-year old answer. But feel free to edit it and fix any broken links.Kelvin
One I really like is Handlerbars.Net the handlebarsjs port github.com/rexm/Handlebars.NetMassimo
P
9

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.

Petasus answered 14/3, 2014 at 15:57 Comment(1)
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.Amphitrite
M
6

More Complete List

  • ASP.Net inbuilt WebForm View Engine
  • ASPView
  • Brail
  • NHaml (.Net port of Haml)
  • Spark
  • NVelocity
  • StringTemplate.Net
Morocco answered 5/3, 2009 at 7:43 Comment(0)
E
6

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.

Electroluminescence answered 5/2, 2011 at 21:18 Comment(0)
C
5

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

Craddock answered 4/12, 2008 at 11:37 Comment(0)
A
4

DotLiquid is very nice templating system for .NET.

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

Austreng answered 3/6, 2014 at 15:34 Comment(0)
P
4

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

Peachey answered 5/11, 2015 at 14:18 Comment(0)
I
3

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/

Inconsumable answered 20/7, 2010 at 0:24 Comment(0)
D
3

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

Doublequick answered 26/6, 2012 at 20:56 Comment(0)
E
2

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.

Ergener answered 22/4, 2011 at 2:28 Comment(1)
Link is dead, new one is- bitethebullet.co.uk/EmailTemplateFramework.aspxIvyiwis
U
2

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>
Udo answered 29/10, 2017 at 0:32 Comment(0)
O
1

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

Odelsting answered 4/12, 2008 at 10:11 Comment(2)
Yes - google found that, but it doesn't look very active. Nothing since 2003. Have you used it?Omnidirectional
its forked by Castle at castleproject.org/castle/projects.htmlMaghutte
R
1

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

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

Hope this helps!!!

Reseta answered 4/12, 2008 at 10:49 Comment(1)
Have you used any the libraries listed in the first link?Omnidirectional
J
1

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

Jevons answered 20/5, 2013 at 3:14 Comment(0)
S
1

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

Sumba answered 4/4, 2014 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.