Added support for Macro Parameters in Razor Macros (@Model.Parameters.AnimalName) where AnimalName is defined on the Macro as a Parameter

This commit is contained in:
agrath@gmail.com
2011-01-26 08:28:16 -13:00
parent 69a1e7000c
commit dca21f9ce1
4 changed files with 67 additions and 6 deletions

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
namespace umbraco.MacroEngines
{
public class DynamicDictionary : DynamicObject
{
Dictionary<string, object> _dictionary;
public DynamicDictionary(Dictionary<string, object> sourceItems)
{
_dictionary = sourceItems;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_dictionary[binder.Name] = value;
return true;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (_dictionary != null)
{
if (_dictionary.TryGetValue(binder.Name.ToLower(), out result))
{
return true;
}
}
result = null;
return true;
}
}
}

View File

@@ -10,6 +10,8 @@ namespace umbraco.MacroEngines
{
public class DynamicNode : DynamicObject, IEnumerable
{
private DynamicDictionary _properties;
private readonly INode n;
public DynamicNode(INode n)
{
@@ -22,7 +24,10 @@ namespace umbraco.MacroEngines
{
//Empty constructor for a special case with Generic Methods
}
public void InitializeProperties(Dictionary<string, object> dict)
{
_properties = new DynamicDictionary(dict);
}
IEnumerable<INode> _children;
public DynamicNode(IEnumerable<INode> children)
{
@@ -373,6 +378,16 @@ namespace umbraco.MacroEngines
return n.ChildrenAsTable(nodeTypeAliasFilter);
}
public DynamicDictionary Parameters
{
get
{
return _properties;
}
set
{
_properties = value;
}
}
}
}

View File

@@ -40,7 +40,7 @@ namespace umbraco.MacroEngines
try
{
string parsedResult;
if (!GetResult(null, code, currentPage, out parsedResult))
if (!GetResult(null, code, currentPage, null, out parsedResult))
{
errorMessage = parsedResult;
return false;
@@ -68,7 +68,7 @@ namespace umbraco.MacroEngines
? macro.ScriptCode
: loadScript(IOHelper.MapPath(SystemDirectories.Python + "/" + macro.ScriptName));
string parsedResult;
GetResult(macro.CacheIdentifier, template, currentPage, out parsedResult);
GetResult(macro.CacheIdentifier, template, currentPage, macro, out parsedResult);
// if it's a file we'll monitor changes to ensure that any updates to the file clears the cache
if (String.IsNullOrEmpty(macro.ScriptCode))
@@ -90,12 +90,23 @@ namespace umbraco.MacroEngines
Razor.SetTemplateBaseType(typeof(UmbracoTemplateBase<>));
}
private bool GetResult(string cacheIdentifier, string template, INode currentPage, out string result)
private bool GetResult(string cacheIdentifier, string template, INode currentPage, MacroModel macroModel, out string result)
{
try
{
Razor.SetTemplateBaseType(typeof(UmbracoTemplateBase<>));
result = Razor.Parse(template, new DynamicNode(currentPage), cacheIdentifier);
DynamicNode Model = new DynamicNode(currentPage);
if (macroModel != null)
{
Dictionary<string, object> Properties = new Dictionary<string, object>();
macroModel.Properties.ForEach(prop => Properties.Add(prop.Key, prop.Value));
Model.InitializeProperties(Properties);
}
else
{
Model.InitializeProperties(new Dictionary<string, object>());
}
result = Razor.Parse(template, Model, cacheIdentifier);
return true;
}
catch (TemplateException ee)

View File

@@ -62,6 +62,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DLRScriptingEngine.cs" />
<Compile Include="DynamicDictionary.cs" />
<Compile Include="DynamicNode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RazorEngine.cs" />