Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Question about custom functions. View modes: 
User avatar
Member
Member
stephen.turley-terradon - 5/25/2012 1:11:47 PM
   
Question about custom functions.
Is it possible to call a custom function in a template's layout much like you can call custom functions in transformations?

User avatar
Member
Member
stephen.turley-terradon - 5/25/2012 1:48:25 PM
   
RE:Question about custom functions.
I'd like to write a function that can look at the current page and return an appropriate class tag for different divs on the page. It would be nice to just call the function at the div class attribute and have it return the appropriate string.

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 5/26/2012 4:55:27 AM
   
RE:Question about custom functions.
Hello,

I would suggest you to use a custom macro for this. You have the context of the current document available within a macro, so you should be able to get he required class name. For more information please check this documentation on custom macros.

Best regards,
Boris Pocatko

User avatar
Member
Member
sgolin-insite - 5/31/2012 4:07:02 PM
   
RE:Question about custom functions.
I am with turley-terradon. I would like to know whether I can call a custom function in an class from a template (like I would a macro) but without using K# or the new macro functionality (which feels much more cumbersome than the old custom macro methodology).

So, for instance, say I want to set the "WHERE" clause for a repeater webpart by placing a call to my custom class static function as the WHERE property value. Is it possible to set that property to something like {% ProcessCustomMacro(" MyClass.DoMyFunction() ", "") %} where DoMyFunction is a static method of a class named MyClass in the AppCode folder?

If not, I'd like to officially request that functionality in releases to come.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 6/1/2012 2:59:15 PM
   
RE:Question about custom functions.
Just add this one custom macro method and you can call all the static methods you want.



///Put this in the RegisterMethids block
MacroMethods.RegisterMethod("InvokeStaticMethod", InvokeStaticMethod, typeof(string), "returns result of static method call.", null, 2, new object[,] { { "type", typeof(int), "The fully qualified type name of the object" }, { "methodName", typeof(int), "The Name of the method to call" }}, null);

//put this outside of the RegisterMethods block
public static object InvokeStaticMethod(params object[] parameters)
{
if (parameters.Length >= 2)
{
Type t = Type.GetType(parameters[0].ToString());
System.Reflection.MethodInfo method = t.GetMethod(parameters[1].ToString(), System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
return method.Invoke(null, parameters.Skip(2).ToArray());
}
return null;
}


You can change the names around if you want, but I just tested it and it works.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 6/1/2012 3:01:37 PM
   
RE:Question about custom functions.
I just wanted to point out that you can call static methods with parameters with this.

return method.Invoke(null, parameters.Skip(2).ToArray());


Notice how I skip the first 2 parameters in the parameter array so it will pass all the other parameters to your method call.