|
||
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) |
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) %}"); } |