API Questions on Kentico API.
Version 6.x > API > Custom Macro View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
Nathoushka - 4/18/2012 10:23:27 AM
   
Custom Macro
I've been trying to get a custom macro to work with Kentico 6.0.25 but I can't seem to find how.

I've code this in a .cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.SettingsProvider;
using CMS.GlobalHelper;

/// <summary>
/// Summary description for CMSModuleLoader
/// </summary>
public partial class CMSModuleLoader
{
private class CLCustomMacroLoaderAttribute : CMSLoaderAttribute
{
/// <summary>
/// Registers module methods.
/// </summary>
public override void Init()
{
// -- Custom macro methods
CLCustomMacroMethods.RegisterMethods();

}
}
}

public static class CLCustomMacroMethods
{
/// <summary>
/// Registers all blog methods to macro resolver.
/// </summary>
public static void RegisterMethods()
{
MacroMethods.RegisterMethod(
"GetTheme", // 1. parameter: Method name.
GetTheme, // 2. parameter: Method delegate (wrapper method).
typeof(string), // 3. parameter: Return type of the method.
"Returns the site theme", // 4. parameter: Comment for the method used in macro autocompletion.
null, // 5. parameter: Formatting string for "human readable" translation of the method call (optional, you do not have to specify).
0, // 6. parameter: Minimal number of parameters needed to call the method (mimimal overload).
null, // 7. parameter: Parameter definition in format {{name, type, comment}, {name, type, comment}, ...}.
null, // 8. parameter: A list of special parameters needed to be supplied by resolver (these parameters are automatically passed by MacroResolver as the first parameters to the wrapper method).
new List<Type>() { typeof(string) } // 9. parameter: List of types for which the method is applicable (set to null for all types to be allowed). When not specified, the method will be applicable to the type of its first parameter.
);
}

public static string GetTheme()
{
return Utils.SiteUtils.GetTheme();
}

public static object GetTheme(params object[] parameters)
{
switch (parameters.Length)
{
default:
return GetTheme();
}
}
}

I was wondering how can I make this work with a macro like this:
{% GetTheme() %}

The method has 0 parameter. It just returns the value that another method returns. I want to use it similar to old K5.5 macros (such as {% ProcessCustomMacro("gettheme", "") %} ).

What's wrong with the code I have?

User avatar
Kentico Support
Kentico Support
kentico_janh - 4/18/2012 11:14:49 AM
   
RE:Custom Macro
Hello,

You don't have to register any custom methods into the macro resolver if you want to only implement your own custom macro. The way, how to create your own macro is very similar to one in version 5.x of Kentico CMS and here is an approach how to do that (in Custom macro section):

http://devnet.kentico.com/docs/devguide/index.html?types_of_macros.htm

Best regards,
Jan Hermann

User avatar
Certified Developer v7
Certified  Developer v7
Nathoushka - 4/18/2012 1:00:47 PM
   
RE:Custom Macro
Okay, so I've modified my code and I have this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.SettingsProvider;
using CMS.GlobalHelper;

/// <summary>
/// Summary description for CMSModuleLoader
/// </summary>
public partial class CMSModuleLoader
{
private class CLCustomMacroLoaderAttribute : CMSLoaderAttribute
{
/// <summary>
/// Registers module methods.
/// </summary>
public override void Init()
{
// -- Custom macro methods
MacroResolver.OnResolveCustomMacro += MacroResolver_OnResolveCustomMacro;
}

private void MacroResolver_OnResolveCustomMacro(object sender, MacroEventArgs e)
{
if (!e.Match)
{
// Add your custom macro evaluation
switch (e.Expression.ToLower())
{
case "theme":
e.Match = true;
e.Result = "spring"; // Will be modified with a method later
break;
}
}
}
}
}


However, it still returns nothing. As if the Init was never happening. Is there something I'm missing?

User avatar
Kentico Support
Kentico Support
kentico_janh - 4/18/2012 1:28:09 PM
   
RE:Custom Macro
Hello,

Strange... I have copied your posted code and it is working fine:

User image

Are you using it in the right custom macro format?

{% ProcessCustomMacro("theme", "") %}

Best regards,
Jan Hermann

User avatar
Certified Developer v7
Certified  Developer v7
Nathoushka - 4/18/2012 1:30:55 PM
   
RE:Custom Macro
Yes, I am using it as described and still nothing.

I have this code in a created .cs file. Does it needs to be in a specific file?

User avatar
Kentico Support
Kentico Support
kentico_janh - 4/18/2012 2:38:15 PM
   
RE:Custom Macro
Hello,

I think, that it needs to be located within the App_Code direcotry, but could you please write your custom macro directly into the App_Code/Samples/Modules/SampleMacroModule.cs file?

Best regards,
Jan Hermann

User avatar
Certified Developer v7
Certified  Developer v7
Nathoushka - 4/18/2012 3:08:57 PM
   
RE:Custom Macro
Well, that worked...

Based on that, I decided to tweak the code a bit. Seems like I had to rename the event (MacroResolver_OnResolveCustomMacro) to something else (CLMacroResolver_OnResolveCustomMacro), as there was a conflict between mine and the Sample one.

By renaming it, it is working perfectly. Why is it often those little things that takes too much time to fix? :S

Thanks for the help.

User avatar
Kentico Support
Kentico Support
kentico_janh - 4/19/2012 1:37:31 AM
   
RE:Custom Macro
Hello,

Yes, that is more than usual :) And I am sorry, even I've forgot to mention it.

Best regards,
Jan Hermann

User avatar
Member
Member
orobalino-tera-la - 5/14/2012 2:20:11 PM
   
RE:Custom Macro
hello there,
i have the same problem.
when i put my custom code in the sample,cs it works but i couldnt make it work in a separated file.
could you post your last codefile?

User avatar
Certified Developer v7
Certified  Developer v7
Nathoushka - 5/14/2012 2:44:12 PM
   
RE:Custom Macro
Hi there,

Of course, I would be glad to help you. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.SettingsProvider;
using CMS.GlobalHelper;
using CMS.CMSHelper;

/// <summary>
/// Summary description for CMSModuleLoader
/// </summary>
[CLCustomMacroLoaderAttribute]
public partial class CMSModuleLoader
{
private class CLCustomMacroLoaderAttribute : CMSLoaderAttribute
{
/// <summary>
/// Registers module methods.
/// </summary>
public override void Init()
{
// -- Custom macro resolving
MacroResolver.OnResolveCustomMacro += CLMacroResolver_OnResolveCustomMacro;
}

/// <summary>
/// Resolves the custom macro
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event arguments</param>
private void CLMacroResolver_OnResolveCustomMacro(object sender, MacroEventArgs e)
{
if (!e.Match)
{
// Add your custom macro evaluation
switch (e.Expression.ToLower())
{
case "theme":
e.Match = true;
e.Result = "spring"; // Change this for your code
break;
}
}
}
}
}


Add this code in a newly created .cs file inside the App_Code directory. You can create a subfolder specific for your site inside and put your file inside it. That should work.

Nathalie

User avatar
Member
Member
orobalin - 5/14/2012 4:56:51 PM
   
RE:Custom Macro
that was very helpful, thanks a lot

User avatar
Certified Developer v7
Certified  Developer v7
Nathoushka - 5/15/2012 11:19:52 AM
   
RE:Custom Macro
You can rename the acronym "CL" which is used inside the code to something else. CL is just the name of the site that it's used in so you can change it to your site's acronym. Just keep something in there to make sure there is no conflict between the sample one.