How to render a single JSON property using jQueryTemplate
Whats the simplest way to render a single JSON pro开发者_Python百科perty using jQueryTemplate?
The simplest would be something like:
<div id="content"></div>
<script id="mainTmpl" type="text/html">
<span>${message}</span>
</script>
<script type="text/javascript">
var data = {
message: "hello world"
};
$("#mainTmpl").tmpl(data).appendTo("#content");
</script>
If you didn't want to define a template, you could even do:
<div id="content"></div>
<script type="text/javascript">
var data = {
message: "hello world"
};
$("<span>${message}</span>").tmpl(data).appendTo("#content");
</script>
If you just passing in a single value, then you could use $data to access it like:
<div id="content"></div>
<script id="mainTmpl" type="text/html">
<span>${$data}</span>
</script>
<script type="text/javascript">
var data = "hello world";
$("#mainTmpl").tmpl(data).appendTo("#content");
</script>
Sample here: http://jsfiddle.net/rniemeyer/2evHS/
精彩评论