Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Writing custom macros with parameters View modes: 
User avatar
Member
Member
jhoppe-gmail - 3/7/2011 4:59:27 PM
   
Writing custom macros with parameters
Hi,

I want to write a simple custom macro that takes a database field (i.e. NewsDate) and formats it (i.e. using String.Format()), so it will have to take a parameter. Can someone post an example using parameters, or provide some help?

I'm not sure how this all comes together. And how do I use it in a web part ({% ProcessCustomMacro("getdateformat", "|(NewsDate) ") %}?

// Add your custom macro evaluation
switch (expression.ToLower())
{
case "GetDateFormat":
match = true;
result = String.Format("dd MMM yyyy", Convert.ToDateTime(sender.CurrentParameters[0, 0]));
break;
}



Thanks!
Joe Hoppe

User avatar
Member
Member
kentico_michal - 3/10/2011 9:14:18 AM
   
RE:Writing custom macros with parameters
Hi Joe,

Could you please describe your request in further detail? Where exactly you want to use this custom macro?

If you need to do so in transformation, I would recommend using Custom transformation method.

Best regards,
Michal Legen

User avatar
Member
Member
jhoppe-gmail - 3/10/2011 6:59:58 PM
   
RE:Writing custom macros with parameters
Hi Michal,

I want to display one date field from the database on a page. Why go through all of the effort of using a repeater when all I want to pull is one date? The macros seem like a great, quick and easy way to display one database field on a page. Why go through all of the effort of creating a repeater and a transformation when all I want is one field and format it in a specific manner?

Is it possible to do so through a custom macro?

Does anyone have a sample macro that takes parameters, so that I can write my own macro that takes the date and a format string, and returns the formatted date?

Thanks!
Joe

User avatar
Member
Member
casey-catalysis - 3/14/2011 2:51:24 PM
   
RE:Writing custom macros with parameters
This is what I did for my custom macro

public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match)
{
match = false;
string result = "";
// Add your custom macro evaluation
//If the macro uses parameters...
if (sender.CurrentParameters.Length > 0)
{
//Add all the parsed parameters and values to a collection to make them easier to access
Dictionary<string, string> parameters = new Dictionary<string, string>();
for (int index = 0; index < sender.CurrentParameters.GetLength(1); index++)
{
parameters.Add(sender.CurrentParameters[index, 0], sender.CurrentParameters[index, 1]);
}

switch (expression.ToLower().Split('|')[0])
{
case "MacroName":
match = true;
result = //Do Stuff;

break;
}
}
//No Params in the macro
else
{}
return result;
}


You probably don't need the Dictionary stuff, but I thought it was easier to access the parameters that way. You could just access them by index from sender.CurrentParameters, but you have to know which ones correspond to which.

User avatar
Member
Member
casey-catalysis - 3/14/2011 7:05:26 PM
   
RE:Writing custom macros with parameters
Whoops, I think you actually want to change sender.CurrentParameters.GetLength(1) to sender.CurrentParameters.GetLength(0)

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 3/15/2011 4:08:43 AM
   
RE:Writing custom macros with parameters
Hi,

If you want to display the newsreleasedate on a news document you do not need to create a custom macro. You could use following one, which will format date into requested form.

{%Format(ToDateTime(newsreleasedate, ""), "{0:dd MMM yyyy}")%}

Best regards,
Ivana Tomanickova

User avatar
Member
Member
jhoppe-gmail - 3/22/2011 11:49:09 AM
   
RE:Writing custom macros with parameters
Ivana,

the documentation makes (todatetime) look like it converts a string to a DateTime. It would have been nice if Kentico documented that more clearly. Anyways, thank so much!

The sample custom macro will be great to have on file for the future as well.

Regards,
Joe Hoppe

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 3/23/2011 3:48:12 AM
   
RE:Writing custom macros with parameters
Hi,

(todatetime) should convert string value to DateTime as well in case the data are in the appropriate format.

Best regards,
Ivana Tomanickova

User avatar
Member
Member
joeh42 - 8/3/2011 9:25:19 AM
   
RE:Writing custom macros with parameters
I forgot how to do this, searched google for an answer, and found my own thread. Thanks again! Works great!

User avatar
Member
Member
jasonl-cba - 8/2/2013 8:38:58 AM
   
RE:Writing custom macros with parameters
This is similar to something I'm trying to do inside a web part in kentico 6.0.51. I want to set custom date format based on ui culture. Everything I've tried has given me errors. Could I use something like this.

{%if (CMSContext.CurrentDocumentCulture = "en-us"){ NABlogDateFormat = 0:MMMM dd, yyyy 'at' h:mm'<span class=\"toLower\">'tt'</span>' } else { NABlogDateFormat = 'Le' + 0:MMMM dd, yyyy 'at' h:mm'<span class=\"toLower\">'tt'</span>' } %}

Not having much luck.