Permalink
Browse files

Simple template includes.

  • Loading branch information...
1 parent 0d3f6a6 commit 7be9e8cf840cb99f2dbc9bce0647fad6e74effca @jdiamond jdiamond committed Oct 28, 2010
View
@@ -7,9 +7,14 @@ public static class Render
{
public static string StringToString(string template, object data)
{
+ return StringToString(template, data, null);
+ }
+
+ public static string StringToString(string template, object data, Func<string, Template> templateLocator)
+ {
var reader = new StringReader(template);
var writer = new StringWriter();
- Template(reader, data, writer);
+ Template(reader, data, writer, templateLocator);
return writer.GetStringBuilder().ToString();
}
@@ -28,11 +33,11 @@ public static void FileToFile(string templatePath, object data, string outputPat
throw new NotImplementedException();
}
- public static void Template(TextReader reader, object data, TextWriter writer)
+ public static void Template(TextReader reader, object data, TextWriter writer, Func<string, Template> templateLocator)
{
var template = new Template();
template.Load(reader);
- template.Render(data, writer);
+ template.Render(data, writer, templateLocator);
}
}
}
@@ -1,3 +1,4 @@
+using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
@@ -9,11 +10,13 @@ public class RenderContext
private readonly Stack<object> _stack = new Stack<object>();
private object _data;
private readonly TextWriter _writer;
+ private readonly Func<string, Template> _templateLocator;
- public RenderContext(object data, TextWriter writer)
+ public RenderContext(object data, TextWriter writer, Func<string, Template> templateLocator)
{
_data = data;
_writer = writer;
+ _templateLocator = templateLocator;
}
public object GetValue(string name)
@@ -96,5 +99,13 @@ public void Write(string text)
{
_writer.Write(text);
}
+
+ public void Include(string templateName)
+ {
+ // TODO: Check for null!
+
+ var template = _templateLocator(templateName);
+ template.Render(this);
+ }
}
}
View
@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -38,19 +39,25 @@ public void Load(TextReader reader)
/// </summary>
/// <param name="data">The data to use to render the template.</param>
/// <param name="writer">The object to write the output to.</param>
+ /// <param name="templateLocator">The delegate to use to locate templates for inclusion.</param>
/// <remarks>
/// The <paramref name="writer" /> is flushed, but not closed or disposed.
/// </remarks>
- public void Render(object data, TextWriter writer)
+ public void Render(object data, TextWriter writer, Func<string, Template> templateLocator)
{
- var context = new RenderContext(data, writer);
+ var context = new RenderContext(data, writer, templateLocator);
+ Render(context);
+
+ writer.Flush();
+ }
+
+ public void Render(RenderContext context)
+ {
foreach (var part in _parts)
{
part.Render(context);
}
-
- writer.Flush();
}
- }
+ }
}
@@ -1,5 +1,3 @@
-using System;
-
namespace Nustache.Core
{
public class TemplateInclude : Part
@@ -13,7 +11,7 @@ public TemplateInclude(string name)
public override void Render(RenderContext context)
{
- throw new NotImplementedException();
+ context.Include(_name);
}
#region Boring stuff
@@ -1,4 +1,5 @@
-using NUnit.Framework;
+using System.IO;
+using NUnit.Framework;
using Nustache.Core;
namespace Nustache.Tests
@@ -114,5 +115,17 @@ public void It_looks_up_the_stack_for_properties()
new { foo = new { /* no bar here */ }, bar = "baz" });
Assert.AreEqual("baz", result);
}
+
+ [Test]
+ public void It_can_include_templates()
+ {
+ var fooTemplate = new Template();
+ fooTemplate.Load(new StringReader("FOO"));
+
+ var result = Render.StringToString(
+ "before{{>foo}}after", null, name => fooTemplate);
+
+ Assert.AreEqual("beforeFOOafter", result);
+ }
}
}
View
@@ -1,6 +1,4 @@
-- Implement template includes.
- - Need a way to prevent infinite recursion.
-- Define a way to look up templates for including.
+- Add a way to prevent infinite template recursion.
- Add tests for ValueGetter.
- Add a syntax for defining templates.
- {{<foo}}template definition goes here{{/foo}}

0 comments on commit 7be9e8c

Please sign in to comment.