Handlebars Expression Helper: output

Output partial HTML as text.

by Gerald Fullam

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
<script id="template" type="text/x-handlebars-template">
    {{output "myPartial" this}}
    {{{output "myPartial" this}}}
</script>
<script id="partial" type="text/x-handlebars-template">	
    <h1>{{id}}</h1>
</script>
<div id="rendered"></div>

JavaScript

Handlebars.registerHelper('output', function(partial, context) {
    var template = Handlebars.compile(Handlebars.partials[partial]);
    return template(context);
});

Handlebars.registerPartial('myPartial', $("#partial").html());

var template = Handlebars.compile($("#template").html());
$("#rendered").html(template(
    {
        "id": "MyCheckboxID"
    }
));