Skip to content

Instantly share code, notes, and snippets.

@mingodad
Created September 7, 2018 11:56
Show Gist options
  • Save mingodad/ccfecf78ae7d8f8ec7b65c9ba4d58eac to your computer and use it in GitHub Desktop.
Save mingodad/ccfecf78ae7d8f8ec7b65c9ba4d58eac to your computer and use it in GitHub Desktop.
//{!first_name}
function fillTemplate(tpl, data)
{
local missing_keys = [];
local result = tpl.gsub(
"{!%s*(%S[^%s}]+)%s*}",
function(key)
{
if(key in data)
{
return data[key];
}
missing_keys.append(key);
return key;
}
);
if(missing_keys.len()) result = missing_keys;
return result;
}
local result = fillTemplate("Hello {!first_name} - I hope everything is going well with you {!first_name}", {"first_name" : "Mike"});
if(type(result) == "array")
{
print("missing keys on data :", result.join(", "));
}
else print(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment