Tips for developing a replacement system

Hi everyone,
I would like advice from the most experienced. :slight_smile:

I am looking for a way to transform a stored string into a database “Best dog food brands for {{{Year}}}” into “Best dog food brands for year(now())”.

But also other functions, for example:

Best dog food brands:

{{{productsID=10,33,90,2222}}}

It returns me a formatted html output.

I hope that you have understood what I mean.
What is the best way to develop such a thing?

Thank you,
Ivan

The strings in the database are already set? If so, can you show a few more examples?

Or, are you designing a new system that will both set and get the values?

For the first example you can do something like the following:

map = {
  "{{{Year}}}" : year(now())
 ,"{{{MonthName}}}" : monthAsString(month(now()))
 ,...
};

formattedString = replace(inputString, map);    // use replaceNoCase for case insensitive match

This style requires more work because you have to parse the values out. I’m not sure if these are 4 separate values or not since you didn’t elaborate much.

You can probably use REGEX to parse that, but whether that’s the best way in this case depends on many factors.

Another approach is to add functions to the map described above, and parse the value before evaluating it in the map. For example:

map = {
  "Year" : function() { return year(now()); }
 ,"MonthName" : function() { return monthAsString(month(now())); }
 ,"productsID" : function(arg) { /* select from db and return formatted string */ }
 ,...
};

// use regex to find the templates:
pattern="{{{[^}]+}}}";
regexResult = refind(pattern, templateString, 1, true);

if (!isEmpty(regexResult.pos)){
  // you have template values, loop over regexResult.pos and regexResult.len
  //  arrays and get substrings from templateString
}

Then each template value should be handled, e.g.

templateValue = replaceList(templateValue, "{{{,}}}", ",");  // remove braces
key = listFirst(templateValue, "=");
arg = (templateValue CT "=") ? listLast(templateValue, "=") : "";

if (map.keyExists(key){
  func = map[key];
  formattedString = func(arg);
}
else {
  // error handling
}
4 Likes

Hi,
Thank you very much for the explanation! It really does seem to me to be a very good solution! :clap:

1 Like