Handlebars
Handlebars is a fantastic templating engine which is available for .NET. It’s similar to Mustache, which I tend to prefer, but Handlebars has more features that can be very useful.
This is a basic example showing how easy it is to apply a template.
// <package id="Handlebars.Net" version="1.7.1" targetFramework="net40" />
// {{{ triple-stash to avoid encoding }}}
using System;
using HandlebarsDotNet;
class Program {
static void Main(string[] args) {
string source =
@"
{{title}}
Hello {{person.name}}, I see that you are {{person.age}} years old.
You have the following cars:
{{#each cars}}
{{year}} {{make}} {{model}} {{#if color}}(Color={{color}}){{/if}}
{{/each}}
";
var template = Handlebars.Compile(source);
var data = new {
title = "My Data Object",
person = new { name = "Billy", age = 34 },
cars = new object[] {
new { year = 2015, make = "Ford", model = "Mustang", color = "Red" },
new { year = 2014, make = "Chevy", model = "Corvette" },
new { year = 2013, make = "Dodge", model = "Aries", color = "Yellow" }
}
};
var result = template(data);
Console.WriteLine(result);
}
}
/****** OUTPUT *******
My Data Object
Hello Billy, I see that you are 34 years old.
You have the following cars:
2015 Ford Mustang (Color=Red)
2014 Chevy Corvette
2013 Dodge Aries (Color=Yellow)
******* OUTPUT ******/
Comments
Post a Comment