Jesper Rønn-Jensen
Jesper Rønn-Jensen

Reputation: 111776

Jquery templates: How to access data object from array

This example does not work for me:

$.tmpl('<span class="ui-button ui-widget ui-icon-${data}">\
        ${data}</span>', \
        ["info", "delete"]\
)

since it will return nothing in place of ${data}. I also tried with ${item} which gave me same result.

What i want to achieve, is to insert the text "info" in the first generated span and "delete" in the next span.

As a workaround, i can pass in objects like so:

[{name: "info"}, {name: "delete"}]

and access them with ${name} directly.

But how to i make it work without the workaround?? What is the correct syntax for getting element in aray?

Upvotes: 0

Views: 917

Answers (1)

Chandu
Chandu

Reputation: 82943

You forgot to add one $ sign before data. Change ${data} to ${$data}

Try this:

$.tmpl('<span class="ui-button ui-widget ui-icon-${$data}">${$data}</span>', ["info", "delete"])

Working example @ http://jsfiddle.net/ythSP/

Upvotes: 2

Related Questions