Skip to content

Instantly share code, notes, and snippets.

@mikeobrien
Created July 11, 2012 22:10
Show Gist options
  • Save mikeobrien/3094047 to your computer and use it in GitHub Desktop.
Save mikeobrien/3094047 to your computer and use it in GitHub Desktop.
Using Nustache
public class Mustache
{
public const string MasterTemplate = @"
<html>
<body>
<h1>TMNT</h1>
<div>{{{Content}}}
</div>
</body>
</html>";
public const string Template = @"
<div class='color: {{#IsGood}}green{{/IsGood}}{{^IsGood}}red{{/IsGood}}'>{{Name}} ({{Status}})</div>
<div>{{#Timestamp}}M/d/yyyy{{/Timestamp}}</div>
<table>
<thead><tr><td>Id</td><td>Name</td></tr></thead>
<tbody>
{{#Items}}
<tr><td>{{Id}}</td><td>{{Name}}</td></tr>
{{/Items}}
</tbody>
</table>";
public const string RenderedTemplate = @"
<html>
<body>
<h1>TMNT</h1>
<div>
<div class='color: green'>Splinter (Good)</div>
<div>7/11/2012</div>
<table>
<thead><tr><td>Id</td><td>Name</td></tr></thead>
<tbody>
<tr><td>1</td><td>Donatello</td></tr>
<tr><td>2</td><td>Leonardo</td></tr>
<tr><td>3</td><td>Raphael</td></tr>
</tbody>
</table>
</div>
</body>
</html>";
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Model
{
public enum StatusType { Good, Bad }
public Model()
{
Items = new List<Item>();
}
public string Name { get; set; }
public StatusType Status { get; set; }
public bool IsGood { get { return Status == StatusType.Good; }}
public List<Item> Items { get; set; }
public Lambda Timestamp = x => DateTime.Now.ToString(x);
}
public static readonly Model ModelInstance = new Model {
Name = "Splinter",
Status = Model.StatusType.Good,
Items = {
new Item { Id = 1, Name = "Donatello" },
new Item { Id = 2, Name = "Leonardo" },
new Item { Id = 3, Name = "Raphael" }
}
};
[Test]
public void should_render_template()
{
var template = Render.StringToString(MasterTemplate, new { Content = Template });
Render.StringToString(template, ModelInstance).ShouldEqual(RenderedTemplate);
}
[Test]
public void render_perf()
{
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
should_render_template();
}
sw.Stop();
Debug.WriteLine(sw.Elapsed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment