Last active
September 1, 2020 09:33
-
-
Save bitsapien/e3130b01d04d57fa4cf5a9200dba50be to your computer and use it in GitHub Desktop.
Single line templating engine
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
/* | |
* Problem: When you want to do very basic templating with very little to no logic, for example templating a mail or an HTML. | |
*/ | |
const render = (templateData, templateHTML) => | |
Object.entries(templateData).reduce( | |
(renderedContent, dataVar) => | |
renderedContent.replace(new RegExp(`{{${dataVar[0]}}}`, 'g'), dataVar[1]), | |
templateHTML | |
); | |
/* Usage | |
* > render({"Name": "Yes"}, "Hello, {{Name}} {{Name}}") | |
* 'Hello, Yes Yes' | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment