I created a custom macro, but for the life of me it won't register. I am using the object explorer view as well and it's not showing up. Can someone shed some light?
Here's my main class (in App_Code/CMS)
using System;
using System.Collections.Generic;
using System.Web;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
/// <summary>
/// Example of custom module with custom macro methods registration.
/// </summary>
public static class GetParentClassMethods
{
/// <summary>
/// Registers all blog methods to macro resolver.
/// </summary>
public static void RegisterMethods()
{
// Register MyMethod with two parameters overload
MacroMethod myMethod = new MacroMethod("getParentCSSColor", getParentCSSColor)
{
Comment = "Scans Parents until Form property 'CSSColor' found and returns value.",
Type = typeof(string),
AllowedTypes = new List<Type>() { typeof(string) },
MinimumParameters = 0
};
myMethod.AddParameter("defaultStyle", typeof(string), "the CSS color (#000000 or inherit) if none found.");
MacroMethods.RegisterMethod(myMethod);
}
#region "Macro methods implementation"
/// <summary>
/// Gets the ancestor CSSColor form element
/// </summary>
public static string getParentCSSColor()
{
return getParentCSSColor("inherit");
}
/// <summary>
/// Scans ancestry and returns non blank CSSColor if found.
/// </summary>
/// <param name="param1">The default style to give if no ancestor with CSSColor found</param>
public static string getParentCSSColor(string defaultStyle)
{
string results = recursiveParentScan(CMS.CMSHelper.CMSContext.CurrentPageInfo);
return string.IsNullOrEmpty(results) ? defaultStyle : results;
}
/// <summary>
/// If CurrentPage has the value "CSSColor" and it's not blank, returns it, otherwise scans parent. Returns blank string if none found in ancestry.
/// </summary>
/// <param name="parentPage">The PageInfo page</param>
/// <returns>the CSSColor value or a blank string if none found in ancestry</returns>
private static string recursiveParentScan(CMS.DocumentEngine.PageInfo currentPage) {
if (currentPage.NodeLevel == 0)
return string.IsNullOrEmpty(currentPage.GetValue("CSSColor").ToString()) ? "" : currentPage.GetValue("CSSColor").ToString();
else
return string.IsNullOrEmpty(currentPage.GetValue("CSSColor").ToString()) ? recursiveParentScan(currentPage.ParentPageInfo) : currentPage.GetValue("CSSColor").ToString();
}
// Add your own custom methods here
#endregion
#region "MacroResolver wrapper methods"
/// <summary>
/// Wrapper method of MyMethod suitable for MacroResolver.
/// </summary>
/// <param name="parameters">Parameters of the method</param>
public static object getParentCSSColor(params object[] parameters)
{
switch (parameters.Length)
{
case 0:
return getParentCSSColor();
case 1:
// Overload with one parameter
return getParentCSSColor(ValidationHelper.GetString(parameters[0], ""));
default:
// No other overload is supported
throw new NotSupportedException();
}
}
// Add wrappers for MacroResolver for your own custom methods here
// The signature of wrapper methods has to be "public static object MyMethodName(params object[] parameters)"
#endregion
}
ANd here's my Loader:
using System;
using CMS.GlobalHelper;
using CMS.OutputFilter;
using CMS.SettingsProvider;
using CMS.IO;
/// <summary>
/// Sample custom module class. Partial class ensures correct registration. For adding new methods, modify SampleModule inner class.
/// </summary>
[SampleMacroLoader]
public partial class CMSModuleLoader
{
/// <summary>
/// Attribute class ensuring correct initialization of methods in macro resolver. You do not need to modify this class.
/// </summary>
private class GetParentClassMacroLoader : CMSLoaderAttribute
{
/// <summary>
/// Registers module methods.
/// </summary>
public override void Init()
{
// -- Custom macro methods
GetParentClassMethods.RegisterMethods();
}
}
}
ANd lastly, here's my caller:
{% ProcessCustomMacro("getParentCSSColor", "") %}
Should return "inherit" if none found, any help?