32
{ Beyond String Concatenation Using jQuery Templating to Cleanly Display Your Data

Beyond String Concatenation

  • Upload
    kitra

  • View
    39

  • Download
    0

Embed Size (px)

DESCRIPTION

Beyond String Concatenation. Using jQuery Templating to Cleanly Display Your Data. Rey Bango. jQuery Team Member Microsoft Client-Web Community PM Editor of ScriptJunkie.com Ajaxian.com Writer Co-host of the jQuery Podcast Co-host of JSConf Live Podcast. Web Templates. - PowerPoint PPT Presentation

Citation preview

Page 1: Beyond String  Concatenation

{Beyond String Concatenation

Using jQuery Templating to Cleanly Display Your Data

Page 2: Beyond String  Concatenation

Rey Bango• jQuery Team Member• Microsoft Client-Web

Community PM  • Editor of ScriptJunkie.com• Ajaxian.com Writer• Co-host of the jQuery Podcast• Co-host of JSConf Live Podcast

Page 3: Beyond String  Concatenation

Web Templates

Page 4: Beyond String  Concatenation

Not a New Invention

• Server-side for years• ASP.NET• Smarty & Savant (PHP)• Cheetah & Mako (Python)

Page 5: Beyond String  Concatenation

Awesome-Sauce• Separates content and

program code• Flexible architecture• Shortens development time• Readability• Great for teams

Page 6: Beyond String  Concatenation

<form action=“clientUpdate.php” method=“post” name=“clUpd”>

Page 7: Beyond String  Concatenation
Page 8: Beyond String  Concatenation

Damnit DOM!• DOM Manipulation x1000• HTML Concatenation Soup• Readability Hell• Maintenance – Needles in

eyes

Page 9: Beyond String  Concatenation

var clRec = "";

for(var i=0; i<clientData.name.length; i++) { clRec += "<li><a href='clients/"+clientData.id[i]+"'>" + clientData.name[i] + "</a></li>";}

$("ul").append(clRec);

HTML Soup

Page 10: Beyond String  Concatenation

<script id=“clientTemplate” type=“text/html”><li><a href=‘clients/${id}’>$

{name}</a></li></script>

$(“#clientTemplate”).tmpl(clientData).appendTo( “ul” );

No Soup for You!

Page 11: Beyond String  Concatenation

Clarity• Affect the UI via pre-built

templates • Nice well-defined structure• Less convoluted and hard to

understand code• Greater maintainability

Page 12: Beyond String  Concatenation

Templates Good• Separates UI from data• JavaScript and HTML-based.

Easy!• JavaScript templates are

rendered and cached client-side• Promotes reusability of existing

templates• Designers & developers can

coexist <3

Page 13: Beyond String  Concatenation

Aren’t server-side templates good

enough?

Page 14: Beyond String  Concatenation

JS Engines

• JavaScript Micro-Templating

• jTemplates• PURE• mustache.js• jQuery Smarty• jQuote

Page 15: Beyond String  Concatenation

jQuery Templating

• 1st contribution from Microsoft

• MIT/GPL – Just like jQuery• Official Plugin supported

by the jQuery Project

Page 16: Beyond String  Concatenation

Data

Template

Templating Engine DOM

Page 17: Beyond String  Concatenation

var tmpl = "<li>${ dataItem }</li>";

A Template

Page 18: Beyond String  Concatenation

<script id=“myTemplate" type="text/html">

<li>${ dataItem }</li></script>

A Template

Page 19: Beyond String  Concatenation

<script id="productsTemplate" type="text/html"> <div> <img src="Content/ProductImages/${Picture}" class="productImage" /> <span class="productName">${Name}</span> Price: ${formatPrice(Price)} <img data-pk="${Id}" src="Content/AddCart.png" alt="Add to Cart" class="addCart" /> </div></script>

A Template

Page 20: Beyond String  Concatenation

.tmpl()

$("#myTemplate").tmpl( dataObject ).appendTo("ul");

<script id=“myTemplate" type="text/html"> <li>${ dataItem }</li></script>

<ul></ul>

Page 21: Beyond String  Concatenation

Code

Page 22: Beyond String  Concatenation

Main Methods

• tmpl()• tmplItem()• template()

- Render the template- Find the template item- Compile/Store a template

Page 23: Beyond String  Concatenation

Supported Tags

• ${...}• {{each

...}}...{{/each}}• {{if ...}}...

{{else ...}}...{{/if}}• {{html ...}} • {{tmpl ...}}• {{wrap

...}}...{{/wrap}}

- Evaluate fields or expression- Loop over template items- Conditional sections- Insert markup from data - Composition, as template items- Composition, plus incorporation of wrapped HTML

Page 24: Beyond String  Concatenation

<script id=“myTemplate" type="text/html">

<li>${parseInt(ReleaseYear) + 100}</li> </script>

Inline Expressions

Page 25: Beyond String  Concatenation

<script id=“myTemplate" type="text/html"><li><a href="clients/${id}">${name}

{{if (age > 30)}} is Old!{{/if}}</a></li></script>

Code Blocks

Page 26: Beyond String  Concatenation

<script id="clientTemplate" type="text/html"> <p><a href="clients/${id}">${name}

{{if (age > 30)}} is Old!{{/if}}</a></p> {{tmpl($data) "#ageTemplate"}}</script>

<script id="ageTemplate" type="text/html">

<p>Current Age: ${age}</p></script>

Nesting

Page 27: Beyond String  Concatenation

function addAge() { return this.data.age + 12;}

….

<script id="ageTemplate" type="text/html"><p>Current Age: ${addAge}</p>

</script>

Custom Functions

Page 28: Beyond String  Concatenation

var clientData = [{ name: "Rey Bango", age: 32, id: 1, phone: [ "954-600-1234", "954-355-5555" ] },…];

….

<ul>{{each

phone}}<li>${$value}</li>{{/each}}</ul>

Looping

Page 29: Beyond String  Concatenation

Code

Page 30: Beyond String  Concatenation

https://github.com/jquery/jquery-tmpl

Make it Better!

Page 31: Beyond String  Concatenation

http://speakerrate.com/reybango

Rate Me!

Page 32: Beyond String  Concatenation

Rey Bango@reybango

[email protected]@microsoft.comhttp://blog.reybango.com