Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Custom Macro - Accessing Parameters View modes: 
User avatar
Member
Member
Jay - 3/13/2010 5:45:46 AM
   
Custom Macro - Accessing Parameters
I have a situation where I need to use a custom macro, and need it to return a true/false to the 'visible' property of a webpart that I have added to a page.

The format of the macro expression is as follows:

{% ProcessCustomMacro("pagecheck", "|(pagename)community") %}

This sets the macro name as 'pagecheck' in addition to a parameter 'pagename' being set to the value 'community'. My objective is to only display the webpart when the word 'community' appears in the document path.

So... i started to work in the CMSCustom.cs file to add the handling of this macro to the custom macro handler.

The problem is that I'm looking for the right way to access the value of the "pagename" parameter without having to do a bunch of string manipulation. Perhaps what I'm looking for doesn't exist, but there must be a better way.

There is a LoadParameter method on the 'sender' object, however the expression that is passed into the function simply contains the entire string as it appears on the webpart configuration screen - so not really useful.

Has anyone done something like this? Any pointers?

It appears that the whole model is pretty open - so for all intents I could just set the expression as "pagecheck|community" and parse it out a little more easiy but then I haven't created very re-usable code, it's a single purpose function and not very good.

Thanks

Jay

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 3/16/2010 8:12:44 AM
   
RE:Custom Macro - Accessing Parameters
Hi Jay,


I would recommend you parsing of string like: http://devnet.kentico.com/Knowledge-Base/API-and-Internals/Using-parameters-in-custom-macros.aspx

You may access the current alias path in the CMSCustom.cs.


Best regards,
Helena Grulichova

User avatar
Member
Member
Vinh - 11/28/2010 7:33:10 PM
   
RE:Custom Macro - Accessing Parameters
I had to do something similar and this is what I came up with.

I needed to parse a custom macro in the forms of
{% ProcessCustomMacro("myexpression", "") %}
{% ProcessCustomMacro("myexpression", "|(not)") %}

In CMSCustom.cs I write the code to handle the parameter using MacroResolver.CurrentParameters.

if(expression.StartsWith("myexpression"))
{
bool value = GetBooleanResult();
if(sender.CurrentParameters.Length > 0)
{
if(sender.currentParameters[0,0] == "not")
value = !value;
}
result = value.ToString();
}

cheers
Vinh Chung

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 11/29/2010 3:47:06 AM
   
RE:Custom Macro - Accessing Parameters
Hello,


I am not sure if your solution works for you. You can also trim the text or use String.EndsWith to get the parameter.


Best regards,
Helena Grulichova

User avatar
Member
Member
Vinh Chung - 11/29/2010 4:16:52 PM
   
RE:Custom Macro - Accessing Parameters
Hi Helena,

thanks. Actually 'not' function is handled by Kentico...so just have to return the boolean value as is.