Dust Js 130210153734 Phpapp01 PDF
Dust Js 130210153734 Phpapp01 PDF
Dust Js 130210153734 Phpapp01 PDF
js } at LinkedIn
Yevgeniy Brikman
2011: LinkedIn adopted dust.js, a
client side templating language
This is the story of client side
templating at massive scale
Dust in the wild
Profile 2.0
Dust in the wild
Influencers
About me
Server sends JSON. The template is fetched from the CDN and
rendered in browser.
Client side rendering (full)
Client side MVC makes client side rendering even more important.
Client side rendering (with MVC)
Once a page has loaded, the client side MVC takes over, fetching
JSON from the server and rendering it with client side templates
Client side rendering benefits
<ul>
<% for(var i = 0; i < supplies.length; i ++) { %>
<li><%= supplies[i] %> </li>
<% } %>
</ul>
<p>
Hello {name}! You have {count} new messages.
</p>
Cons
● Very little usage outside of Google. No plans to push new versions or
accept new contributions.
● Some functionality is missing, such as being able to loop over maps.
● Not DRY: adding new functionality requires implementing plugins in both
Java and JavaScript.
Mustache
Pros
● Very popular choice with a large, active community.
● Server side support in many languages, including Java.
● Logic-less templates do a great job of forcing you to separate presentation
from logic.
● Clean syntax leads to templates that are easy to build, read, and maintain.
Cons
● A little too logic-less: basic tasks (e.g. label alternate rows with different
CSS classes) are difficult.
● View logic is often pushed back to the server or implemented as a
"lambda" (callable function).
● For lambdas to work on client and server, you must write them in
JavaScript.
● Slow, interpreted templates
Handlebars
Pros
● Logic-less templates do a great job of forcing you to separate presentation
from logic.
● Clean syntax leads to templates that are easy to build, read, and maintain.
● Compiled rather than interpreted templates.
● Better support for paths than mustache (ie, reaching deep into a context
object).
● Better support for global helpers than mustache.
Cons
● Requires server-side JavaScript to render on the server.
Dust.js
Pros
● Logic-less templates do a great job of forcing you to separate presentation
from logic.
● Clean syntax leads to templates that are easy to build, read, and maintain.
● Compiled rather than interpreted templates.
● Better support for paths than mustache (ie, reaching deep into a context
object).
● Better support for global helpers than mustache.
● Inline parameters.
● Blocks & inline partials.
● Overriding contexts.
● Support for asynchronous rendering and streaming.
● Composable templates.
Cons
● Requires server-side JavaScript to render on the server.
● Maintainer of github repo is not responsive.
Spoiler alert!
dust.js won
Takeaways
● Based on how we weighed our criteria, Dust
fit our needs the best
● Homepage:
http://linkedin.github.com/dustjs/
Logic
Homework assignment: implement
this view with a truly logic-less
template (no helpers/lambdas!)
Helpers to the rescue: @eq, @ne
{@select key=age}
{@eq value="1"}Baby{/eq}
{@lt value="10"}Child{/lt}
{@lt value="18"}Teen{/lt}
{@default}Adult{/default}
{/select}
Helpers to the rescue: @size, @math
<p>
<a href="${url.link( 'home-page' }">$!{i18n('hello-world' )}</a>
</p>
json.put("name", "Jim");
json.put("home-page-link" , Url.link("home-page" ));
json.put("hello-world-text" , I18n.get("hello-world" ));
render("profile-page" , json);
<p>
<a href="{home-page-link} ">{hello-world-text} </a>
</p>
All i18n, text formatting, and URL generation is done server side
and added to the JSON payload
Option #1: everything server side
Pros
● Simple, easy to understand
● Clean templates
Cons
● Controller code cluttered with view logic
Option #2: dynamic pre-processing
Original profile-page dust template
<p>
<a href="{@pre.link key="home-page"}">{@pre.i18n key="hello-world"}</a>
</p>
<p>
<a href="{link-123}">{i18n-456}</a>
</p>
Step 1: the @pre helper tags get replaced at build time with
references to unique keys in the JSON
Option #2: dynamic pre-processing
Java controller
json.put("name", "Jim");
render("profile-page" , json);
Pre-processed JSON
{
"name": "Jim",
"link-123" : "http://www.linkedin.com" ,
"i18n-456" : "Hello World"
}
Pros
● All view logic is in the templates
● Clean server side code
Cons
● Complicated, hard to debug
● Tight coupling: need special server and build
logic to use templates
● Performance: increased JSON payload
and/or more server processing time
Option #3: static pre-processing
Original profile-page dust template
<p>
<a href="{home-page-link}">{@i18n}Hello World{/i18n}</a>
</p>
<p>
<a href="{home-page-link}">Bonjour monde</a>
</p>
Pros
● Hybrid approach: i18n is in the templates,
only formatting/link generation is in controller
● Simpler, easier to debug than dynamic pre-
processing
Cons
● Custom build process
● Increased template payload, but i18n strings
now cached with template
Outline
<html>
<body>
<h1>Composable UI </h1>
<script type="fs/embed" fs-uri="/news-feed/top" ></script>
<script type="fs/embed" fs-uri="/pymk"></script>
<script type="fs/embed" fs-uri="/ad"></script>
</body>
</html>
If Fizzy finds an fs/embed in the HTML, it calls the URI and injects
the response into the page.
Fizzy
HTML skeleton
Embed
Embed
Embed
Dust
template
Dust template
Dust
template
Dust template
Typical page
HTML Skeleton
Dust
template
Dust template
Dust
The fold template
Dust template
On initial page load, the user doesn't see anything below the fold
Typical page
HTML Skeleton
Dust
template
Dust template
Dust
The fold template
No need
to render Dust template
No need to fetch
data or render
Deferred rendering