Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Creating Custom Macros View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 6/8/2012 4:18:46 AM
   
Creating Custom Macros
Hi Guys,

I am creating some custom macros in our website.

Only the default one which says {#someexpression#} is working while all the others are not working.

Below is my code:

using System;

using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.CMSOutputFilter;
using CMS.GlobalHelper;

/// <summary>
/// Sample custom module class. Partial class ensures correct registration. For adding new methods, modify SampleModule inner class.
/// </summary>
[SampleMacroLoader]
public partial class CMSModuleLoader
{
#region "Macro methods loader attribute"

/// <summary>
/// Attribute class ensuring correct initialization of methods in macro resolver. You do not need to modify this class.
/// </summary>
private class SampleMacroLoaderAttribute : CMSLoaderAttribute
{
/// <summary>
/// Registers module methods.
/// </summary>
public override void Init()
{
// -- Custom macro methods
//CustomMacroMethods.RegisterMethods();

// -- Custom macro resolving
MacroResolver.OnResolveCustomMacro += MacroResolver_OnResolveCustomMacro;

// -- Custom output substitution resolving
//OutputFilter.OnResolveSubstitution += OutputFilter_OnResolveSubstitution;
}


/// <summary>
/// Resolves the output substitution
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event arguments</param>
private void OutputFilter_OnResolveSubstitution(object sender, CMS.OutputFilter.SubstitutionEventArgs e)
{
if (!e.Match)
{
// Add your custom macro evaluation
switch (e.Expression.ToLower())
{
case "somesubstitution":
e.Match = true;
e.Result = "Resolved substitution";
break;
}
}
}


/// <summary>
/// Resolves the custom macro
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event arguments</param>
private void MacroResolver_OnResolveCustomMacro(object sender, MacroEventArgs e)
{
if (!e.Match)
{
// Add your custom macro evaluation
switch (e.Expression.ToLower())
{
case "TestExpression":
e.Match = true;
e.Result = "Resolved expression";
break;

case "someexpression":
e.Match = true;
e.Result = "Resolved expression";
break;

case "FullURL":
e.Match = true;
string sFullURL = CMS.GlobalHelper.URLHelper.RawUrl;
e.Result = sFullURL;
break;

case "WithoutQueryParameters":
e.Match = true;
string sWithoutQuery = CMS.GlobalHelper.URLHelper.RemoveQuery(CMS.GlobalHelper.URLHelper.RawUrl);
e.Result = sWithoutQuery;
break;

case "OnlyQueryParameters":
e.Match = true;
string sOnlyQueryParameters = CMS.GlobalHelper.URLHelper.GetQuery(CMS.GlobalHelper.URLHelper.RawUrl);
e.Result = sOnlyQueryParameters;
break;
}
}
}
}

#endregion
}




Any ideas why would {#TestExpression#} not work?

Thanks
Gitesh Shah


User avatar
Member
Member
kentico_michal - 6/8/2012 4:30:05 AM
   
RE:Creating Custom Macros
Hi Gitesh,

I think the reason is that you convert the string to lowercase:

switch (e.Expression.ToLower())

which is fine, but in that case you need to specify values to compare in lowercase, such as:

case "testexpression":

Best regards,
Michal Legen

User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 6/8/2012 4:31:16 AM
   
RE:Creating Custom Macros
Thanks Michal,

You saved me.

Cheers
Gitesh Shah