From dca21f9ce18da268adacfba83652a2c702ac75ff Mon Sep 17 00:00:00 2001 From: "agrath@gmail.com" Date: Wed, 26 Jan 2011 08:28:16 -1300 Subject: [PATCH] Added support for Macro Parameters in Razor Macros (@Model.Parameters.AnimalName) where AnimalName is defined on the Macro as a Parameter --- .../DynamicDictionary.cs | 34 +++++++++++++++++++ umbraco.MacroEngines.Juno/DynamicNode.cs | 19 +++++++++-- umbraco.MacroEngines.Juno/RazorEngine.cs | 19 ++++++++--- .../umbraco.MacroEngines.csproj | 1 + 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 umbraco.MacroEngines.Juno/DynamicDictionary.cs diff --git a/umbraco.MacroEngines.Juno/DynamicDictionary.cs b/umbraco.MacroEngines.Juno/DynamicDictionary.cs new file mode 100644 index 0000000000..ad16edf8b5 --- /dev/null +++ b/umbraco.MacroEngines.Juno/DynamicDictionary.cs @@ -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 _dictionary; + public DynamicDictionary(Dictionary 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; + } + } +} diff --git a/umbraco.MacroEngines.Juno/DynamicNode.cs b/umbraco.MacroEngines.Juno/DynamicNode.cs index 1ffe57d000..14c7cd265b 100644 --- a/umbraco.MacroEngines.Juno/DynamicNode.cs +++ b/umbraco.MacroEngines.Juno/DynamicNode.cs @@ -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 dict) + { + _properties = new DynamicDictionary(dict); + } IEnumerable _children; public DynamicNode(IEnumerable children) { @@ -373,6 +378,16 @@ namespace umbraco.MacroEngines return n.ChildrenAsTable(nodeTypeAliasFilter); } - + public DynamicDictionary Parameters + { + get + { + return _properties; + } + set + { + _properties = value; + } + } } } diff --git a/umbraco.MacroEngines.Juno/RazorEngine.cs b/umbraco.MacroEngines.Juno/RazorEngine.cs index 0c9341fb51..ae8df43d6c 100644 --- a/umbraco.MacroEngines.Juno/RazorEngine.cs +++ b/umbraco.MacroEngines.Juno/RazorEngine.cs @@ -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 Properties = new Dictionary(); + macroModel.Properties.ForEach(prop => Properties.Add(prop.Key, prop.Value)); + Model.InitializeProperties(Properties); + } + else + { + Model.InitializeProperties(new Dictionary()); + } + result = Razor.Parse(template, Model, cacheIdentifier); return true; } catch (TemplateException ee) diff --git a/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj b/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj index dd3454dd8c..24b354bdea 100644 --- a/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj +++ b/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj @@ -62,6 +62,7 @@ +