Skip to content

Instantly share code, notes, and snippets.

@mikeobrien
Created July 12, 2012 00:41
Show Gist options
  • Save mikeobrien/3094763 to your computer and use it in GitHub Desktop.
Save mikeobrien/3094763 to your computer and use it in GitHub Desktop.
Using Spark
[TestFixture]
public class Spark
{
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 writer = new StringWriter();
MessageBuilder.Current.Transform("Hello ${#Name}", new { Name = "Fark" }, writer);
writer.ToString().ShouldEqual(RenderedTemplate);
}
[Test]
public void render_perf()
{
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 2; i++)
{
should_render_template();
}
sw.Stop();
Debug.WriteLine(sw.Elapsed);
}
public class DefaultMessageBuilder : MessageBuilder
{
private readonly ISparkViewEngine _engine;
public DefaultMessageBuilder()
{
var settings = new SparkSettings();
settings.SetPageBaseType(typeof(TemplateBase));
settings.AddViewFolder(ViewFolderType.FileSystem, new Dictionary<string, string> { { "basePath", Environment.CurrentDirectory } });
_engine = new SparkViewEngine(settings);
}
public override void Transform(string template, object data, TextWriter output)
{
const string path = "~template";
File.WriteAllText(path, template);
var descriptor = new SparkViewDescriptor().AddTemplate(path);
var view = (TemplateBase)_engine.CreateInstance(descriptor);
try
{
view.ViewData = data;
view.RenderView(output);
}
finally
{
_engine.ReleaseInstance(view);
File.Delete(path);
}
}
}
public abstract class MessageBuilder
{
private static MessageBuilder _instance;
public static MessageBuilder Current
{
get
{
return _instance ??
Interlocked.CompareExchange(ref _instance, new DefaultMessageBuilder(), null) ??
_instance;
}
set { _instance = value; }
}
public abstract void Transform(string message, object data, TextWriter output);
public string Transform(string message, object data)
{
var writer = new StringWriter();
Transform(message, data, writer);
return writer.ToString();
}
}
}
public abstract class TemplateBase : AbstractSparkView
{
public object ViewData { get; set; }
public object Eval(string expression)
{
return DataBinder.Eval(ViewData, expression);
}
public string Eval(string expression, string format)
{
return DataBinder.Eval(ViewData, expression, format);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment