26

Is there an existing library to render handlebar templates in .NET?

I would like to use this as a templating engine for users to create HTML email templates

I've spent a few hours looking, but can't seem to find anything. A wrapped up javascript rendering model would be ok, but a native library better.

Otherwise, is there a similar templating engine more suited to the .NET environment?

Paul Grimshaw
  • 19,894
  • 6
  • 40
  • 59

6 Answers6

36

Since this question was answered, a full-blown, native .NET library for Handlebars became available (disclosure: I am the primary author).

Handlebars.Net on GitHub and on NuGet

It compiles Handlebars templates to IL, and has excellent feature coverage.

Rex M
  • 142,167
  • 33
  • 283
  • 313
9

Typical, shortly after posting I think I found the solution:

https://github.com/jehugaleahsa/mustache-sharp#readme

The 'Mustache' in the name confused me, but in fact this library is based on the handlebars syntax.

I have since started my own Fork of this library, to enable "Greater Than/Less Than/Equal To" tags:

https://github.com/PaulGrimshaw/mustache-sharp

Paul Grimshaw
  • 19,894
  • 6
  • 40
  • 59
  • 1
    Did you saw my answer? Handlebars is based on Mustache templating engine. The project you linked just add some syntactic sugar on top of Mustache; it has roughly the same capabilities. – Simon Boudrias Apr 04 '13 at 23:24
  • Mustache# is actually built on Handlebars: 'mustache# brings the power of handlebars.js to .NET and then takes it a little bit further. Not only does it support the same tags, it also handles whitespace intelligently.' – Paul Grimshaw Apr 05 '13 at 09:06
  • 2
    The link you posted is an adaptation of Mustache - not Mustache itself. Note I'm not telling you not to use this template engine - it looks all fine, and I prefer the syntactic sugar (semantic) Handlebars provide on top Mustache. I was just hoping to clarify where both stand in the figure. – Simon Boudrias Apr 05 '13 at 13:57
  • 2
    "mustache-sharp" is an alternative to handlebars, not an extension or adaptation. It has some distinct incompatibilities - you cannot lift a handlebars template wholesale and drop it into mustache-sharp and expect it to work the same. That may or may not be fine, depending on the project. But they are definitely not the same thing. Handlebars.Net is an at-parity implementation of the handlebars language (https://github.com/rexm/handlebars.net) – Rex M Jun 04 '16 at 21:07
2

Another option is a wrapper of the actual handlebars.js library in C# via the V8 bindings. https://github.com/pressf12/handlebars.cs

Nathan
  • 601
  • 1
  • 6
  • 8
0

If you want to use Handlebar server side then have a look at node.js and these two questions:

Node.js with Handlebars.js on server and client

Connecting to a node.js server from C#

Community
  • 1
  • 1
DaveRead
  • 3,371
  • 1
  • 21
  • 24
  • Thanks Dave. I was hoping there might be a simpler way of doing this without having to run Node.js,and the Mustache# library appears to do this – Paul Grimshaw Apr 05 '13 at 09:11
  • To update my answer, 3 years have passed and now there is a server-side Handlebars view renderer available https://github.com/rexm/handlebars.net – DaveRead Mar 21 '16 at 11:31
0

It seems like you would prefer to use handlebar views server side, but if you want to compile them server side, in C#, and render them client side then I have found 2 options:

Community
  • 1
  • 1
Adam Gordon Bell
  • 3,083
  • 2
  • 26
  • 53
-1

Handlebars have really been made for user inside the browsers. I don't think it's a good idea to use it on the server (Even node.js don't have a server side Handlebars compiler)

Although, Handlebars base syntax is derived from the Mustache templating engine. And, Mustache have direct support for .NET!

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
  • Thanks for the answer, but had found Mustache already. I was specifically looking for Handlebars on the server rather than Mustache. Can you elaborate on why you don't think it's a good idea to use Handlebars on the server? – Paul Grimshaw Apr 05 '13 at 09:08
  • Because Handlebars is really think for the browser first. As so, I feel it misses some really important feature on the server like layout management, etc. I feel Handlebars is really think to create "patials" view, not full scalable HTML architecture. That's why on Node.js most Handlebars' fan use Hogan.js and Mustache. – Simon Boudrias Apr 05 '13 at 14:01
  • 1
    There're several Handlerbars viewengine modules to use in Node.js. This is my favourite: https://github.com/ericf/express3-handlebars – David Merrilees May 25 '13 at 12:54