Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>&lt;!-- Install jQuery and underscore --&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js"&gt;&lt;/script&gt; &lt;!-- Create your template --&gt; &lt;script type="foo/bar" id='usageList'&gt; &lt;table cellspacing='0' cellpadding='0' border='1' &gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Id&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;% // repeat items _.each(items,function(item,key,list){ // create variables var f = item.name.split("").shift().toLowerCase(); %&gt; &lt;tr&gt; &lt;!-- use variables --&gt; &lt;td&gt;&lt;%= key %&gt;&lt;/td&gt; &lt;td class="&lt;%= f %&gt;"&gt; &lt;!-- use %- to inject un-sanitized user input (see 'Demo of XSS hack') --&gt; &lt;h3&gt;&lt;%- item.name %&gt;&lt;/h3&gt; &lt;p&gt;&lt;%- item.interests %&gt;&lt;/p&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% }); %&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/script&gt; &lt;!-- Create your target --&gt; &lt;div id="target"&gt;&lt;/div&gt; &lt;!-- Write some code to fetch the data and apply template --&gt; &lt;script type="text/javascript"&gt; var items = [ {name:"Alexander", interests:"creating large empires"}, {name:"Edward", interests:"ha.ckers.org &lt;\nBGSOUND SRC=\"javascript:alert('XSS');\"&gt;"}, {name:"..."}, {name:"Yolando", interests:"working out"}, {name:"Zachary", interests:"picking flowers for Angela"} ]; var template = $("#usageList").html(); $("#target").html(_.template(template,{items:items})); &lt;/script&gt; </code></pre> <ul> <li><a href="http://jsfiddle.net/aPv9H/625/">JsFiddle</a> Thanks @PHearst!</li> <li><a href="http://jsfiddle.net/aPv9H/626/">JsFiddle</a> (latest)</li> <li><a href="http://jsfiddle.net/bnSmh/">JsFiddle</a> List grouped by first letter (complex example w/ images, function calls, sub-templates) fork it! have a blast...</li> <li><a href="http://jsfiddle.net/7jWuD/16/">JsFiddle</a> Demo of XSS hack noted by @tarun_telang below</li> <li><a href="http://jsfiddle.net/23uTD/1/">JsFiddle</a> One non-standard method to do sub-templates</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload