Handlebars custom partial resolution
by AndrewLeedham
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.js"></script>
<pre id="log"></pre>
<div id="output"></div>
<script type="template/handlebars" id="partial">{{data}}</script>
<script type="template/handlebars" id="template">Hello {{> partial}}</script>
JavaScript
const originalCompile = Handlebars.compile;
Handlebars.compile = (input, options) => {
const temp = originalCompile(input, options);
return (context, execOptions) => {
document.getElementById('log').innerHTML = execOptions && Object.keys(execOptions);
return temp(context, execOptions);
}
}
Handlebars.registerPartial('partial', document.getElementById('partial').innerHTML);
var template = Handlebars.compile(document.getElementById('template').innerHTML);
document.getElementById('output').innerHTML = template({data: 'World!'});