Skip to content

Commit

Permalink
Merge pull request #36 from scottnickel/release-3.0
Browse files Browse the repository at this point in the history
Release 3.0 pull request
  • Loading branch information
Matthew Abbott committed Mar 21, 2012
2 parents b64ed7a + 3ac2219 commit 1a3f70f
Show file tree
Hide file tree
Showing 22 changed files with 2,602 additions and 3,640 deletions.
3,613 changes: 1,598 additions & 2,015 deletions nuget/lib/net40/RazorEngine.XML

Large diffs are not rendered by default.

Binary file modified nuget/lib/net40/RazorEngine.dll
Binary file not shown.
Binary file modified nuget/lib/net40/RazorEngine.pdb
Binary file not shown.
336 changes: 262 additions & 74 deletions src/Core/RazorEngine.Core/Razor.cs

Large diffs are not rendered by default.

117 changes: 115 additions & 2 deletions src/Core/RazorEngine.Core/Templating/DynamicViewBag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace RazorEngine.Templating
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;

Expand All @@ -12,7 +14,8 @@ public class DynamicViewBag : DynamicObject
private readonly IDictionary<string, object> _dict = new Dictionary<string, object>();
#endregion

#region Methods
#region DynamicObject Overrides

/// <summary>
/// Gets the set of dynamic member names.
/// </summary>
Expand Down Expand Up @@ -53,6 +56,116 @@ public override bool TrySetMember(SetMemberBinder binder, object value)

return true;
}

#endregion

#region Helper Methods

/// <summary>
/// Add a value to this instance of DynamicViewBag.
/// </summary>
/// <param name="propertyName">
/// The property name through which this value can be get/set.
/// </param>
/// <param name="value">
/// The value that will be assigned to this property name.
/// </param>
public void AddValue(string propertyName, object value)
{
if (propertyName == null)
throw new ArgumentNullException("The propertyName parameter may not be NULL.");

if (_dict.ContainsKey(propertyName) == true)
throw new ArgumentException("Attempt to add duplicate value for the '" + propertyName + "' property.");

_dict.Add(propertyName, value);
}

/// <summary>
/// Adds values from the specified valueList to this instance of DynamicViewBag.
/// </summary>
/// <param name="valueList">
/// A list of objects. Each must have a public property of keyPropertyName.
/// </param>
/// <param name="keyPropertyName">
/// The property name that will be retrieved for each object in the specified valueList
/// and used as the key (property name) for the ViewBag. This property must be of type string.
/// </param>
public void AddListValues(IList valueList, string keyPropertyName)
{
foreach (object value in valueList)
{
if (value == null)
throw new ArgumentNullException("Invalid NULL value in initializer list.");

Type type = value.GetType();
object objKey = type.GetProperty(keyPropertyName);

if (objKey.GetType() != typeof(string))
throw new ArgumentNullException("The keyPropertyName property must be of type string.");

string strKey = (string)objKey;

if (_dict.ContainsKey(strKey) == true)
throw new ArgumentException("Attempt to add duplicate value for the '" + strKey + "' property.");

_dict.Add(strKey, value);
}
}

/// <summary>
/// Adds values from the specified valueDictionary to this instance of DynamicViewBag.
/// </summary>
/// <param name="valueDictionary">
/// A dictionary of objects. The Key of each item in the dictionary will be used
/// as the key (property name) for the ViewBag.
/// </param>
public void AddDictionaryValues(IDictionary valueDictionary)
{
foreach (object objKey in valueDictionary.Keys)
{
if (objKey.GetType() != typeof(string))
throw new ArgumentNullException("The Key in valueDictionary must be of type string.");

string strKey = (string)objKey;

if (_dict.ContainsKey(strKey) == true)
throw new ArgumentException("Attempt to add duplicate value for the '" + strKey + "' property.");

object value = valueDictionary[strKey];

_dict.Add(strKey, value);
}
}

/// <summary>
/// Adds values from the specified valueDictionary to this instance of DynamicViewBag.
/// </summary>
/// <param name="valueDictionary">
/// A generic dictionary of {string, object} objects. The Key of each item in the
/// dictionary will be used as the key (property name) for the ViewBag.
/// </param>
/// <remarks>
/// This method was intentionally not overloaded from AddDictionaryValues due to an ambiguous
/// signature when the caller passes in a Dictionary&lt;string, object&gt; as the valueDictionary.
/// This is because the Dictionary&lt;TK, TV&gt;() class implements both IDictionary and IDictionary&lt;TK, TV&gt;.
/// A Dictionary&lt;string, ???&gt; (any other type than object) will resolve to AddDictionaryValues.
/// This is specifically for a generic List&lt;string, object&gt;, which does not resolve to
/// an IDictionary interface.
/// </remarks>
public void AddDictionaryValuesEx(IDictionary<string, object> valueDictionary)
{
foreach (string strKey in valueDictionary.Keys)
{
if (_dict.ContainsKey(strKey) == true)
throw new ArgumentException("Attempt to add duplicate value for the '" + strKey + "' property.");

object value = valueDictionary[strKey];

_dict.Add(strKey, value);
}
}

#endregion
}
}
}
26 changes: 25 additions & 1 deletion src/Core/RazorEngine.Core/Templating/ExecuteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,34 @@
/// </summary>
public class ExecuteContext
{
#region Constructors

/// <summary>
/// Creates a new instance of ExecuteContext with an empty ViewBag.
/// </summary>
public ExecuteContext()
{
_viewBag = new DynamicViewBag();
}

/// <summary>
/// Creates a new instance of DynamicViewBag, setting initial values in the ViewBag.
/// </summary>
/// <param name="viewBag">The initial view bag data or NULL for an empty ViewBag.</param>
public ExecuteContext(DynamicViewBag viewBag)
{
if (viewBag == null)
_viewBag = new DynamicViewBag();
else
_viewBag = viewBag;
}

#endregion

#region Fields
private readonly IDictionary<string, Action> _definedSections = new Dictionary<string, Action>();
private readonly Stack<TemplateWriter> _bodyWriters = new Stack<TemplateWriter>();
private readonly dynamic _viewBag = new DynamicViewBag();
private readonly dynamic _viewBag;
#endregion

#region Properties
Expand Down
Loading

0 comments on commit 1a3f70f

Please sign in to comment.