Search code examples
jqueryknockout.jsjquery-templates

Knockout template name building


I am using jQuery.tmpl and Knockout. Inside jQuery tmpl template I am using Knockout binding as follows:

<div data-bind="template: { name: 'field-String'}"></div>

I need to have dynamic template name. How can I achieve this?

I tried with no luck:

<div data-bind="template: { name: 'field-${Type}'}"></div>

Solution

  • You don't need the jQuery.tmpl syntax here, you can just use plain string concatenation to build your template name:

    <div data-bind="template: { name: 'field-' + Type }"></div>
    

    Note: if your Type is an ko.observable you need to write: name: 'field-' + Type()