Custom Settings module resolve macros

Dennis Hulsmans asked on January 5, 2017 14:59

Hi

We're trying to create a custom settings module. Within this module there is a setting where we put a small snippet (javascript code) which uses another custom setting.

For example:

{% Settings.MyCustomSettingsScript %}

This will render the javascript code. Within the javascript code we need a key "mycode-123abc", which we are trying to get via a macro as well ({% Settings.MyCustomSettingsAPIKey %}. This key is a custom setting as well.

Is this possible or is there an easier solution?

KR, D.

Recent Answers


Brenden Kehren answered on January 5, 2017 15:15

So you have a global piece of generic of JS code stored and you're making it dynamic by allowing that code to also have macros in it? Why couldn't you use the macro engine or a simple string.replace() to resolve the dynamic piece in that global code? I think what you're talking about would work. Downside is every time you call it, it requires at least 2 calls to the database so make sure you have caching setup.

2 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on January 5, 2017 16:29

Technically you can resolve any macro as long as you have enough info in your macro context. Macro essentially is a replacement. For example you need to update description for a given tree node and description contains macro:

TreeNode node = ... // init your node for what you want to update the description
string descriptionWithMacro = " {%CurrentDocument.DocumentName@%} has description... bla bla " 
MacroResolver resolver = MacroResolver.GetInstance();
resolver.SetNamedSourceData("CurrentDocument", node); // Set CurrentDocument to node
string desc = resolver.ResolveMacros(descriptionWithMacro);
node.SetValue("MyDescriptionField", desc); // update 

If you don't have enough context you need provide it i.e. For {% Settings.MyCustomSettingsAPIKey %} "Setting" must be known for MacroResolver. If it is not known you can do resolver.SetNamedSourceData("Settings", YourSettingOjectWithCustomSettingsScriptProperty)

P.S. There is a GlobalMacroResolver, which is probably aware of the "settings" object so you might just do MacroContext.GlobalResolver.ResolveMacros().

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.