Created
September 7, 2018 11:56
-
-
Save mingodad/ccfecf78ae7d8f8ec7b65c9ba4d58eac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//{!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