A bit of a hack, but you could grab the innerHTML
and replace template variables e.g. surrounded by double-curly braces.
I recommend avoiding this, and instead use a templating library such as handlebars
const
title = 'Title',
desc = 'Description',
substitute = (text, replacements) =>
text.replace(/\{\{(\w+)\}\}/g, (g0, g1) => replacements[g1]),
substituteHtml = (el, replacements) =>
el.innerHTML = substitute(el.innerHTML, replacements);
substituteHtml(document.body, { title, desc });
<h1>{{title}}</h1>
<p>{{desc}}</p>
A better alternative
Example templating library usage using handlebars. This let's us build/modify HTML content safely.
const
context = {
title: 'Title',
desc: 'Description'
},
template = document.querySelector('#handlebars-demo').innerHTML,
templateFn = Handlebars.compile(template);
document.querySelector('.output').innerHTML = templateFn(context);
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script>
<div class="output"></div>
<script id="handlebars-demo" type="text/x-handlebars-template">
<h1>{{title}}</h1>
<p>{{desc}}</p>
</script>