Can jQuery template bind to a raw array

user1684 picture user1684 · May 24, 2011 · Viewed 7.1k times · Source

Given

<div id="place_holder" />

<script id="template" type="text/x-jquery-tmpl">
WHAT DO I PUT HERE
</script>

var array_of_ints = [1,2,3]

$( "#template" )
        .tmpl( array_of_ints   )
        .appendTo( "#place_holder" );

What can I put within the template to get ?

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>

Answer

nidx picture nidx · Jul 4, 2011
<div id="place_holder" />

<script id="template" type="text/x-jquery-tmpl">
    {{= $data}}
</script>

var array_of_ints = [1,2,3]

$( "#template" )
    .tmpl( array_of_ints   )
    .appendTo( "#place_holder" );

{{= $data}} works everywhere ( ${} wont work in strings in the template )