Skip to content

Instantly share code, notes, and snippets.

@bitsapien
Last active September 1, 2020 09:33
Show Gist options
  • Save bitsapien/e3130b01d04d57fa4cf5a9200dba50be to your computer and use it in GitHub Desktop.
Save bitsapien/e3130b01d04d57fa4cf5a9200dba50be to your computer and use it in GitHub Desktop.
Single line templating engine
/*
* 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