Require Template in View-Model and convert to Raw HTML

In the view-model we pass in options to a select2 instance:

const select2Options = {
    templateResult: item => { return `<div><span>${item}</span></div>` }
}

Where I’d like to require the template instead of using raw strings:

import Select2Template from './select2-template.html'

const select2Options = {
    templateResult: Select2Template
}

With the template declaring the binding values:

<template bindable="item">
    <div>
        <span>${item}</span>
    </div>
</template>

Even if I have to construct the template with a callback function, that would be fine because it would allow me to create more manageable code.

1 Like