Portal Engine Questions on portal engine and web parts.
Version 4.x > Portal Engine > custom macro return html as a string View modes: 
User avatar
Member
Member
Fabien Calais - 12/15/2010 4:09:43 AM
   
custom macro return html as a string
Hi,

I have a custom macro which return html text inside the invoice template
but the html is display as a string (<table><td>....)

I saw on other topics that I have to use sender.AllowParameters = false;

But this property seems to not exist in Kentico CMS 4.0

Any other solution?


User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 12/15/2010 1:14:09 PM
   
RE:custom macro return html as a string
Hi,

You could use the (encode)false parameter to display the correct HTML output of your custom macro. Problem could be that we had a bug in the 4.0 version that these macro parameters did not work correctly for custom macros. It was fixed in the 4.1 version that is why I would recommend you to upgrade .

If the upgrade is not possible for you here is a code sample of workaround:



/// <summary>
/// Custom macro handler
/// </summary>
/// <param name="sender">Sender (active macro resolver)</param>
/// <param name="expression">Expression to resolve</param>
/// <param name="match">Returns true if the macro matches (was resolved)</param>
public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match)
{
match = false;
string result = expression;

// Add your custom macro evaluation
switch (expression.ToLower())
{
case "prepare": // {% ProcessCustomMacro("prepare", "") %}{%test|(encode)false%}
match = true;
result = "";
sender.OnGetValue += new MacroResolver.OnGetValueEventHandler(sender_OnGetValue);
break;
}

return result;
}


static object sender_OnGetValue(string name)
{
// Add your custom macro evaluation
switch (name.ToLower())
{
case "test": // {% ProcessCustomMacro("prepare", "") %}{%test|(encode)false%}
return "<strong> Strong text </strong>";
}
return null;
}


Then you could use this expression to call your macro: {% ProcessCustomMacro("prepare", "") %}{%test|(encode)false%}

Best regards,
Ivana Tomanickova

User avatar
Member
Member
Fabien Calais - 12/16/2010 3:05:35 AM
   
RE:custom macro return html as a string
Hi,

Thanks for the workaround;
I can reproduce your sample code

But I need help to use this workaround with a more complexe custom macro

here under my custom macro


public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match)
{
match = false;
string result = expression;
switch (expression.ToLower())
{
case "productlist.invoice":
match = true;
result = GetProductList(sender.SourceObject as ShoppingCartInfo,"fr-fr");
break;
}

return result;
}


how i have to rewrite this to use your workaround?


User avatar
Member
Member
Fabien Calais - 12/16/2010 8:35:23 AM
   
RE:custom macro return html as a string
Hi,

Its ok now, I find how to do (new in c# :)


public static class CMSCustom
{
private static MacroResolver vSender = new MacroResolver();

public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match)
{
match = false;
string result = expression;
vSender = sender;
switch (expression.ToLower())
{
case "html":
match = true;
result = "";
sender.OnGetValue += new MacroResolver.OnGetValueEventHandler(sender_OnGetValue);
break;
}

return result;
}

static object sender_OnGetValue(string name)
{
// Add your custom macro evaluation
switch (name.ToLower())
{
case "productlist.invoice": ;
return GetProductList(vSender.SourceObject as ShoppingCartInfo);
}
return null;
}


I'm not sure this is a valid c# code but it works !
now, i can have the table of the product list inside my invoice