<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>22516</bug_id>
          
          <creation_ts>2013-06-29 08:09:48 +0000</creation_ts>
          <short_desc>Improve the &lt;template&gt; example</short_desc>
          <delta_ts>2013-07-03 19:41:32 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WHATWG</product>
          <component>HTML</component>
          <version>unspecified</version>
          <rep_platform>Other</rep_platform>
          <op_sys>other</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc>http://www.whatwg.org/specs/web-apps/current-work/#the-template-element</bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P3</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>Unsorted</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>contributor</reporter>
          <assigned_to name="Ian &apos;Hixie&apos; Hickson">ian</assigned_to>
          <cc>ian</cc>
    
    <cc>mathias</cc>
    
    <cc>mike</cc>
          
          <qa_contact>contributor</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>90018</commentid>
    <comment_count>0</comment_count>
    <who name="">contributor</who>
    <bug_when>2013-06-29 08:09:48 +0000</bug_when>
    <thetext>Specification: http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html
Multipage: http://www.whatwg.org/C#the-template-element
Complete: http://www.whatwg.org/c#the-template-element
Referrer: http://t.co/tLwpI6EdML

Comment:
Improve the &lt;template&gt; example

Posted from: 78.20.174.76 by mathias@qiwi.be
User agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1546.0 Safari/537.36</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>90019</commentid>
    <comment_count>1</comment_count>
    <who name="Mathias Bynens">mathias</who>
    <bug_when>2013-06-29 08:14:27 +0000</bug_when>
    <thetext>Currently the example says:

    &lt;!DOCTYPE html&gt;
    &lt;title&gt;Cat data&lt;/title&gt;
    &lt;script&gt;
     // Data is hard-coded here, but could come from the server
     var data = [
       { name: &apos;Pillar&apos;, color: &apos;Ticked Tabby&apos;, sex: &apos;Female (neutered)&apos;, legs: 3 },
       { name: &apos;Hedral&apos;, color: &apos;Tuxedo&apos;, sex: &apos;Male (neutered)&apos;, legs: 4 },
     ];
    &lt;/script&gt;
    &lt;table&gt;
     &lt;thead&gt;
      &lt;tr&gt;
       &lt;th&gt;Name &lt;th&gt;Color &lt;th&gt;Sex &lt;th&gt;Legs
     &lt;tbody&gt;
      &lt;template id=&quot;row&quot;&gt;
       &lt;tr&gt;&lt;td&gt;&lt;td&gt;&lt;td&gt;&lt;td&gt;
      &lt;/template&gt;
    &lt;/table&gt;
    &lt;script&gt;
     var template = document.querySelector(&apos;#row&apos;);
     for (var i in data) {
       var cat = data[i];
       var clone = template.content.cloneNode(true);
       var cells = clone.querySelectorAll(&apos;td&apos;);
       cells[0].textContent = cat.name;
       cells[1].textContent = cat.color;
       cells[2].textContent = cat.sex;
       cells[3].textContent = cat.legs;
       template.parentNode.appendChild(clone);
     }
    &lt;/script&gt;

Using `for…in` to iterate over arrays in JavaScript is generally a bad idea, though. It doesn’t guarantee index order, and the loop not only iterates over all enumerable properties of the object itself, but also those the object inherits from its constructor’s prototype (properties closer to the object in the prototype chain override prototypes’ properties), which may lead to issues. 

To avoid confusing developers reading the spec, I would suggest using `Array#forEach` instead:

    &lt;script&gt;
     var template = document.querySelector(&apos;#row&apos;);
     data.forEach(function(cat) {
       var clone = template.content.cloneNode(true);
       var cells = clone.querySelectorAll(&apos;td&apos;);
       cells[0].textContent = cat.name;
       cells[1].textContent = cat.color;
       cells[2].textContent = cat.sex;
       cells[3].textContent = cat.legs;
       template.parentNode.appendChild(clone);
     });
    &lt;/script&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>90253</commentid>
    <comment_count>2</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2013-07-03 19:39:18 +0000</bug_when>
    <thetext>I&apos;ll just use a numeric for loop.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>90255</commentid>
    <comment_count>3</comment_count>
    <who name="">contributor</who>
    <bug_when>2013-07-03 19:41:32 +0000</bug_when>
    <thetext>Checked in as WHATWG revision r8025.
Check-in comment: make this example script more resilient
http://html5.org/tools/web-apps-tracker?from=8024&amp;to=8025</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>