micro templates for javascript norman white. background separate html code from data provide a...

19
Micro Templates for Javascript Norman White

Upload: bartholomew-hoover

Post on 23-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Micro Templates for Javascript

Norman White

Page 2: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Background

• Separate HTML code from data • Provide a flexible reusable template to generate

html with embedded data• Reduce the amount of code that needs to be

written• Provide ability to develop standard templates for

web applications• Original article– http://ejohn.org/blog/javascript-micro-templating/

Page 3: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Original MicroTemplate• // Simple JavaScript Templating

// John Resig - http://ejohn.org/ - MIT Licensed(function(){ var cache = {}; this.tmpl = function tmpl(str, data){ // Figure out if we're getting a template, or if we need to // load the template - and be sure to cache the result. var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) : // Generate a reusable function that will serve as a template // generator (and which will be cached). new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};" + // Introduce the data as local variables using with(){} "with(obj){p.push('" + // Convert the template into pure JavaScript str .replace(/[\r\t\n]/g, " ") .split("<%").join("\t") .replace(/((^|%>)[^\t]*)'/g, "$1\r") .replace(/\t=(.*?)%>/g, "',$1,'") .split("\t").join("');") .split("%>").join("p.push('") .split("\r").join("\\'") + "');}return p.join('');"); // Provide some basic currying to the user return data ? fn( data ) : fn; };})();

Page 4: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Tricks that allow micro templates

• “Hide” the template definition in a script type that the browsers don’t recognize, then call the micro template function to dynamically fill in the data elements.

• Use Javascript’s powerful regular expression facilities to create a tiny templating expansion function that is passed a template and data, and generates an html string that can then be used somewhere.

• You can precompile the templating function for performance.

Page 5: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Example Template

• <script type="text/html" id="item_tmpl"> <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>"> <div class="grid_1 alpha right"> <img class="righted" src="<%=profile_image_url%>"/> </div> <div class="grid_6 omega contents"> <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p> </div> </div></script>

Page 6: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Example Template

• <script type="text/html" id="item_tmpl"> <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>"> <div class="grid_1 alpha right"> <img class="righted" src="<%=profile_image_url%>"/> </div> <div class="grid_6 omega contents"> <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p> </div> </div></script>

NAME

Page 7: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Example Template

• <script type="text/html" id="item_tmpl"> <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>"> <div class="grid_1 alpha right"> <img class="righted" src="<%=profile_image_url%>"/> </div> <div class="grid_6 omega contents"> <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p> </div> </div></script>

Data Element

Page 8: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Usage

var results = document.getElementById("results");results.innerHTML = tmpl("item_tmpl", dataObject);

QUESTION: What is in “dataObject” ?

Page 9: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Many micro templates now

• Jquery Templates• Mustache• Pure• Mootools• Whiskers• …

Page 10: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

MustachePopular micro template

Used in Dreamweaver examples

• “Logic-less”– No if, else, for , …

• 300 lines of Javascript code• Works in python, ruby or javascript– Can run on server side or browser

Page 11: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Typical Template

Hello {{name}} You have just won ${{value}}! {{#in_ca}} Well, ${{taxed_value}}, after taxes. {{/in_ca}}

Page 12: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Data Object

{ "name": "Chris", "value": 10000, "taxed_value": 10000 - (10000 * 0.4), "in_ca": true }

Page 13: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Result

Hello Chris You have just won $10000! Well, $6000.0, after taxes.

Page 14: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Usage

Mustache only uses “tags”, no logic

Tag TypesVariables

{{name}} {{{name}}} (unescape html)Sections – renders blocks of text one or more times

{{#person}} …. {{/person}}Lambdas – Callable objects, i.e. functions

Page 15: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Some Mustache ExamplesTemplate

{{name}} {{age}} {{company}} {{{company}}}

Hash (input data){“name”: “Chris”,“company”: “<b>GitHub</b>”{Output

Chris

&lt;b&gt;GitHub&lt;/b&gt; <b>GitHub</b>

Page 16: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Lists(Note automatic iteration)

Template:– {{#repo}} – <b>{{name}}</b> – {{/repo}}

Hash:{ "repo": [

{ "name": "resque" },

{ "name": "hub" }, { "name": "rip" },

] }

Output:<b>resque</b> <b>hub</b> <b>rip</b>

Page 17: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Lambdas• Template:

– {{#wrapped}} – {{name}} is awesome. – {{/wrapped}}

• Hash:– { – "name": "Willy", – "wrapped": function() { – return function(text) { – return "<b>" + render(text) + "</b>" } – } – }

• Output:– <b>Willy is awesome.</b>

Page 18: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Invoking MustacheJust call Mustache.render(template, data)

• Include mustache.js

• var view = { • title: "Joe", • calc: function () { • return 2 + 4; • } • };

• var output = Mustache.render("{{title}} spends {{calc}}", view);

• Output = “Joe spends 6”

Page 19: Micro Templates for Javascript Norman White. Background Separate HTML code from data Provide a flexible reusable template to generate html with embedded

Typical Uses

• Get data from server as a JSON string• Decode JSON data and pass to a mustache

template to display.

• Many examples of Mustache on-line