Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTMLBars, how do I even?

Godfrey Chan
November 03, 2015

HTMLBars, how do I even?

Three Handlebars templates walk into one of the newly-opened HTMLBars on the block. After having a few drinks, they each put down a $100 bill.

The renderer said to them, “sorry, no change” and left the DOM untouched.

Godfrey Chan

November 03, 2015
Tweet

More Decks by Godfrey Chan

Other Decks in Programming

Transcript

  1. Three Handlebars templates walk into one of the newly-opened HTMLBars

    on the block. After having a few drinks, they each put down a $100 bill.
  2. var list = document.getElementById("todos"); for (var i = 0; i

    < todoItems.length; i++) { var item = todoItems[i]; var row = document.createElement("li"); var content = document.createTextNode(item.title); if (item.completed) { var del = document.createElement("del"); del.appendChild(content); row.appendChild(del); } else { row.appendChild(content); } list.appendChild(row); }
  3. var list = document.getElementById("todos"); var html = ""; for (var

    i = 0; i < todoItems.length; i++) { var item = todoItems[i]; html += "<li>"; if (item.completed) { html += "<del>" + item.title + "</del>"; } else { html += item.title; } html += "</li>"; } list.innerHTML = html;
  4. var completedItem = "<li><del>{{title}}</del></li>"; var incompleteItem = "<li>{{title}}</li>"; var list

    = document.getElementById("todos"); var html = ""; for (var i = 0; i < todoItems.length; i++) { var item = todoItems[i]; var template = item.completed ? completedItem : incompleteItem; html += template.replace("{{title}}", item.title); } list.innerHTML = html;
  5. function(todoItems) { var html = ""; html += "<ul>"; for

    (var i = 0; i < todoItems.length; i++) { var item = todoItems[i]; html += "<li>"; if (item.completed) { html += "<del>" + item.title + "</del>"; } else { html += item.title; } html += "</li>"; } html += "</ul>"; return html; } <ul> {{#each items as |item|}} {{#if item.completed}} <li><del>{{item.title}}</del></li> {{else}} <li>{{item.title}}</li> {{/if}} {{/each}} </ul> Compiler todos.hbs todos.js
  6. <script id="metamorph-173-start" type="text/x-placeholder"></script> <li><del> <script id="metamorph-174-start" type="text/x-placeholder"></script> Finish HTMLBars Talk

    <script id="metamorph-174-end" type="text/x-placeholder"></script> </del></li> <script id="metamorph-173-end" type="text/x-placeholder"></script> {{#if item.completed}} <li><del>{{item.title}}</del></li> {{else}} <li>{{item.title}}</li> {{/if}}
  7. function(todoItems) { var list = document.createElement("ul"); for (var i =

    0; i < todoItems.length; i++) { var item = todoItems[i]; var row = document.createElement("li"); var content = document.createTextNode(item.title); if (item.completed) { var del = document.createElement("del"); del.appendChild(content); row.appendChild(del); } else { row.appendChild(content); } list.appendChild(row); } return list; } <ul> {{#each items as |item|}} {{#if item.completed}} <li><del>{{item.title}}</del></li> {{else}} <li>{{item.title}}</li> {{/if}} {{/each}} </ul> Compiler todos.hbs todos.js
  8. <svg width="{{width}}" height="{{height}}"> <rect x="0" y="0" width="{{width}}" height="{{height}}" fill="#1B1B1B" />

    {{#each points as |point|}} <circle cx="{{point.x}}" cy="{{point.y}}" r="{{point.size}}" stroke-width="1" stroke="lightblue" stroke-opacity="{{point.opacity}}" fill-opacity="0" /> {{/each}} </svg>
  9. var TodoList = React.createClass({ render() { var items = [];

    this.props.todoItems.forEach(function (item) { if (item.completed) { items.push(<li><del>{ item.title }</del></li>); } else { items.push(<li>{ item.title }</li>); } }); return <ul>{ items }</ul>; } });
  10. function render(data) { elementOpen('ul'); todoItems.forEach(function (item) { elementOpen('li'); if (item.completed)

    { elementOpen('del'); } text(item.title); if (item.completed) { elementClose('del'); } elementClose('li'); }); elementClose('ul'); }
  11. <h1>{{post.title}}</h1> <h3 class="post-author"> <i class="fa fa-user"></i> Written by {{post.author}} </h3>

    <h3 class="post-date"> <i class="fa fa-calendar"></i> Published {{relative-time post.published_at}} </h3> <hr> <div class="post-body"> {{post.body}} </div>
  12. <div> <div> <div> <div> <div> <div> <div> <div> <div> <div>

    {{post.title}} </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
  13. <div> <div> <div> <div> <div> <div> <div> <div> <div> <div>

    Oh hai! </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
  14. <h1>{{post.title}}</h1> <h3 class="post-author"> <i class="fa fa-user"></i> Written by {{post.author}} </h3>

    <h3 class="post-date"> <i class="fa fa-calendar"></i> Published {{relative-time post.published_at}} </h3> <hr> <div class="post-body"> {{post.body}} </div> [ { node: [TextNode], statement: "{{post.title}}", lastValue: "Rails is omakase" }, { node: [TextNode], statement: "{{relative-time post.published_at}}", lastValue: "3 days ago" }, { node: [TextNode], statement: "{{post.body}}", lastValue: "There are lots of à la carte software..." } ]
  15. [ { node: [TextNode], statement: "{{post.title}}", lastValue: "Rails is omakase"

    } ] <div> <div> <div> <div> <div> <div> <div> <div> <div> <div> {{post.title}} </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
  16. [ // Nothing to diff! ] <div> <div> <div> <div>

    <div> <div> <div> <div> <div> <div> Oh hai! </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
  17. <h1>{{post.title}}</h1> <h3 class="post-author"> <i class="fa fa-user"></i> Written by {{post.author}} </h3>

    <h3 class="post-date"> <i class="fa fa-calendar"></i> Published {{relative-time post.published_at}} </h3> <hr> <div class="post-body"> {{post.body}} </div> [ { node: [TextNode], statement: "{{post.title}}", lastValue: "Rails is omakase" }, { node: [TextNode], statement: "{{relative-time post.published_at}}", lastValue: "3 days ago" }, { node: [TextNode], statement: "{{post.body}}", lastValue: "There are lots of à la carte software..." } ]
  18. <h1>{{post.title}}</h1> <h3 class="post-author"> <i class="fa fa-user"></i> Written by {{post.author}} </h3>

    <h3 class="post-date"> <i class="fa fa-calendar"></i> Published {{relative-time post.published_at}} </h3> <hr> <div class="post-body"> {{post.body}} </div> [ { node: [TextNode], reference: { value(): …, isDirty(): … }, lastValue: "Rails is omakase" }, { node: [TextNode], reference: { value(): …, isDirty(): … }, lastValue: "3 days ago" }, { node: [TextNode], reference: { value(): …, isDirty(): … }, lastValue: "There are lots of à la carte software..." } ]