Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery templates, escape "${...}"

I have a piece of javascript code as follows:

var data = { ... };
var template = "<select>" +
                 "<option value='${0:###,###.##}'>Format as $</option>" + 
               "</select>";

$.tmpl(template, data).appendTo("#placeholder");

My problem is that I want to evaluate the "value='${0:###,###.##}'" as a string, but jQuery templates attempts to evaluate it as an object reference.

Is there a way to escape the ${} characters?

Cheers, Shane

like image 673
Shane Avatar asked Dec 31 '25 22:12

Shane


1 Answers

Try to replace $ with its special HTML character &#36;

var data = { };
var template = "<select>" +
         "<option value='&#36;{0:###,###.##}'>Format as $</option>" + 
       "</select>";

$.tmpl( template, data).appendTo("#placeholder");
like image 118
Sergey Ratnikov Avatar answered Jan 02 '26 11:01

Sergey Ratnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!