Permalink
Browse files

Added support for paths in variable references.

  • Loading branch information...
jdiamond committed Feb 11, 2012
1 parent 18c08e7 commit 1b5ea230363a39ce6ab7c0e3300668e5c9200ced
@@ -25,6 +25,18 @@ public void It_renders_the_named_value_from_the_context()
Assert.AreEqual("b", writer.GetStringBuilder().ToString());
}
+ [Test]
+ public void It_supports_accessing_members_of_child_objects()
+ {
+ var a = new VariableReference("a.b");
+ var writer = new StringWriter();
+ var context = new RenderContext(null, new { a = new { b = "c" } }, writer, null);
+
+ a.Render(context);
+
+ Assert.AreEqual("c", writer.GetStringBuilder().ToString());
+ }
+
[Test]
public void It_has_a_useful_ToString_method()
{
@@ -34,7 +34,7 @@ public int Compare(object x, object y)
if (x.GetType() == typeof(VariableReference))
{
- return ((VariableReference)x).Name.CompareTo(((VariableReference)y).Name);
+ return ((VariableReference)x).Path.CompareTo(((VariableReference)y).Path);
}
return -1;
@@ -27,9 +27,9 @@ public RenderContext(Section section, object data, TextWriter writer, TemplateLo
_includeLevel = 0;
}
- public object GetValue(string name)
+ public object GetValue(string path)
{
- if (name == ".")
+ if (path == ".")
{
return _dataStack.Peek();
}
@@ -38,7 +38,7 @@ public object GetValue(string name)
{
if (data != null)
{
- var value = ValueGetter.GetValue(data, name);
+ var value = GetValueFromPath(data, path);
if (value != null)
{
@@ -50,9 +50,26 @@ public object GetValue(string name)
return null;
}
- public IEnumerable<object> GetValues(string name)
+ private static object GetValueFromPath(object data, string path)
{
- object value = GetValue(name);
+ var names = path.Split('.');
+
+ foreach (var name in names)
+ {
+ data = ValueGetter.GetValue(data, name);
+
+ if (data == null)
+ {
+ return null;
+ }
+ }
+
+ return data;
+ }
+
+ public IEnumerable<object> GetValues(string path)
+ {
+ object value = GetValue(path);
if (value is bool)
{
@@ -6,48 +6,50 @@ namespace Nustache.Core
{
public class VariableReference : Part
{
- private readonly string _name;
- private readonly bool _escaped;
- private static readonly Regex Inner = new Regex(@"^\{(.+?)\}$");
- public VariableReference(string name)
+ private static readonly Regex _notEscapedRegex = new Regex(@"^\{(.+?)\}$");
+ private readonly string _path;
+ private readonly bool _escaped;
+
+ public VariableReference(string path)
{
- if (name == null)
+ if (path == null)
{
- throw new ArgumentNullException("name");
+ throw new ArgumentNullException("path");
}
- _name = name;
- var match = Inner.Match(name);
- _escaped = !match.Success;
- if (match.Success)
- {
- _name = match.Groups[1].Value;
- }
+ _path = path;
+ var match = _notEscapedRegex.Match(path);
+ _escaped = !match.Success;
+
+ if (match.Success)
+ {
+ _path = match.Groups[1].Value;
+ }
}
- public string Name { get { return _name; } }
+ public string Path { get { return _path; } }
public override void Render(RenderContext context)
{
- object value = context.GetValue(_name);
+ object value = context.GetValue(_path);
if (value != null)
{
- context.Write(_escaped
- ? HttpUtility.HtmlEncode( value.ToString())
- : value.ToString());
+ context.Write(_escaped
+ ? HttpUtility.HtmlEncode(value.ToString())
+ : value.ToString());
}
}
public override string Source()
{
- return "{{" + _name + "}}";
+ return "{{" + _path + "}}";
}
public override string ToString()
{
- return string.Format("VariableReference(\"{0}\")", _name);
+ return string.Format("VariableReference(\"{0}\")", _path);
}
}
}

0 comments on commit 1b5ea23

Please sign in to comment.