19
Client-side rendering using mustache.js BHASHKAR SHARMA 01 Oct' 11

Client side rendering using mustache.js

Embed Size (px)

DESCRIPTION

My jsfoo presentation describing how to use client-side rendering using mustache.js and the use we put it to, for making TouchSites.

Citation preview

Page 1: Client side rendering using mustache.js

Client-side rendering using mustache.js

 BHASHKAR SHARMA01 Oct' 11

Page 2: Client side rendering using mustache.js

About me

Core Team Member at MobStac

Working on mobile web for 1.5 years

Love front-end development and studying User Experience

B. Tech. from College Of Engineering Roorkee

Page 3: Client side rendering using mustache.js

TouchSite

Page 4: Client side rendering using mustache.js

The Classical web

ServerClient

nth request

ServerClient

(n+1)th request

template

datapage

page

template

data

Load page

Load page

Page 5: Client side rendering using mustache.js

The AJAX web

ServerClient

nth request

ServerClient

(n+1)th request

template

datapage

markup

template

data

Load page

Add markup to page

Page 6: Client side rendering using mustache.js

Rendering on the server

markuptemplate data

Rendering on the client

markuptemplate data

server

server server client

Page 7: Client side rendering using mustache.js

The client-rendered web

ServerClient

nth request

ServerClient

(n+1)th request

template dataRender,

Add markup to page

template

datapageLoad page

Page 8: Client side rendering using mustache.js

JQuery Mobile

• Fixed page layout• Request full page, break it down and load it via AJAX (counter-

intuitive)• Too much overhead overriding default behavior

Page 9: Client side rendering using mustache.js

Enter mustache.js

 

Page 10: Client side rendering using mustache.js

Why mustache

• High performance• Available in multiple languages• Similar to django templates

Ruby, JavaScript, Python, Erlang, PHP, Perl, Objective-C, Java, .NET, Android, C++, Go, Lua, ooc, ActionScript, ColdFusion, Scala, Clojure, Fantom, CoffeeScript, D, and node.js

{

Page 11: Client side rendering using mustache.js

Benefits of client-side rendering

• Data independent of layout• Unified data API• Can cache responses• Save server cycles• Save bandwidth

server

tablet foneBrowser

same data

Websites AND native apps

Page 12: Client side rendering using mustache.js

A mustache template<div>

This will {{action}} once.

<ul>

{{#repeat}}

<li>The count is {{count}} and the word is {{word}}.</li>

{{/repeat}}

</ul>

{{#blank}}

This would not be displayed.

{{/blank}}

</div>

Page 13: Client side rendering using mustache.js

Data

{

“action” : “print”,

“repeat” : [

{ “count” : “1”, “word” : “Apple” },

{ “count” : “2”, “word” : “Gravity” },

{ “count” : “3”, “word” : “Newton” }

]

}

Page 14: Client side rendering using mustache.js

Rendered template

<div>

This will print once.

<ul>

<li>The count is 1 and the word is Apple.</li>

<li>The count is 2 and the word is Gravity.</li>

<li>The count is 3 and the word is Newton.</li>

</ul>

</div>

Page 15: Client side rendering using mustache.js

DEMO

Page 16: Client side rendering using mustache.js

Code Examples

def getTemplateJson(request):

template_json = {

'contents' : getTemplate('ajax_contents.html'),

'article' : getTemplate('ajax_article.html')

}

return HttpResponseJSON(simplejson.dumps(template_json))

Page 17: Client side rendering using mustache.js

Code Examples

def articleView(request, article_id):

article = get_object_or_404(Article, pk=article_id)

return HttpResponseJSON(simplejson.dumps(article, ensure_ascii=False, cls=CustomJSONEncoder))

Page 18: Client side rendering using mustache.js

The Future

• Allow readers to customize layout by picking themes or repositioning elements

• Make elements more interactive• Use client-side rendering for mobile phones

(based on device capability)

(Python with mustache)

Page 19: Client side rendering using mustache.js

My handle is @bhashkarsharma and I'm not a terrorist geekSpeed-Post: [email protected]

Thank You!

We are looking for code buddhas to join our team