Last active
April 29, 2023 18:31
-
-
Save signalwerk/39b5ee219ce9c3228d89712236fd4629 to your computer and use it in GitHub Desktop.
Replace {{ values }} in String
This file contains 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
// This is a basic templating function. It replaces any {{key}} in the | |
// string with the corresponding value in data. If no data is provided, | |
// then the template is returned as-is. | |
// | |
// Example: | |
// template("Hello, {{name}}!", { name: "John" }) → "Hello, John!" | |
// template("Hello, {{name}}!") → "Hello, {{name}}!" | |
export function template(string, data = {}) { | |
let out = string; | |
for (const [key, value] of Object.entries(data)) { | |
out = out.replace(`{{${key}}}`, value); | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment