Kentico CMS 6.0 Developer's Guide

Resolving macros using API

Resolving macros using API

Previous topic Next topic Mail us feedback on this topic!  

Resolving macros using API

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

There is an easy way to resolve macros in .NET code. To resolve all the macros (recommended), use the following static method:

 

string CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros(string inputText)

 

To resolve just the localization macros, use another static method:

 

string CMS.GlobalHelper.ResHelper.LocalizeString(string inputText)

 

Creating a custom resolver

 

In the following example, you can see how a custom macro resolver can be created, loaded with data and used to resolve a text containing macros.

 

using CMS.SiteProvider;

using CMS.CMSHelper;

 

private void CustomResolverExample()

{

  string resolvedText = "";

 

  // Create child resolver of ContextResolver

  ContextResolver resolver = CMSContext.CurrentResolver.CreateContextChild();

 

  // Fill the resolver with custom data

   resolver.SetNamedSourceData("MyUser", UserInfoProvider.GetUserInfo("administrator"));

   resolver.SourceParameters = new object[,] { { "MySimpleValue", "RESOLVED Simple value!" }, { "MySecondSimpleValue", "RESOLVED Second simple value!" } };

 

  // Use the resolver to resolve macros in the text

   resolvedText = resolver.ResolveMacros("Name of my user is (in upper case): {% MyUser.UserName.ToUpper() %}. First eight characters of simple val#1: {% MySimpleValue.Substring(0, 8) %}");

}