SlideShare a Scribd company logo
Gran Sasso Science Institute
Ivano Malavolta
Handlebars
& Require JS
Roadmap
•  Introduction
•  Require JS
•  Handlebars
•  Conclusions
Some technical advices from a real project...
Some technical advices from a real project...
Some technical advices from a real project...
Some technical advices from a real project...
Some technical advices from a real project...
How you structure your applications
MVC framework for
giving structure
File and module loader
for code modularization
Templating engine for
separation of concerns
How you structure your applications
Roadmap
•  Introduction
•  Require JS
•  Handlebars
•  Conclusions
Require JS
•  Why Require JS
•  Using modules
•  Defining modules
•  Configuring Require JS
Why Require JS
We are building apps, not websites
We need well-specified and isolated JS files/modules
Code complexity grows as the app gets bigger
à we need some sort of #include/import/require	
  
à ability to load nested dependencies
What we want to avoid
uncontrolled scripts
poor control flow understanding
RequireJS
RequireJS is a JavaScript file and module loader
Using a modular script loader like RequireJS will improve the modularity of your code
à  speed in implementing changes
à  better undestanding of the code
Require JS allows modules to be loaded as fast as possible, even out of order, but evaluated in the
correct dependency order
Built on the Module Pattern
JavaScript file and module loader
The module pattern
A JavaScript code module is some JavaScript code located in a registered location (e.g., a function)
All of the code that runs inside the function lives in a closure, which provides
•  privacy
•  state
throughout the lifetime of the module
Module example
Technically, it is simply a function that executes immediately
Module VS script files
A module is different from a traditional script file in that it defines a well-scoped object that avoids
polluting the global namespace à its retained objects can be deleted by the GC
It can explicitly list its dependencies and get a handle on those dependencies without needing to refer to
global objects, but instead receive the dependencies as arguments to the function that defines the
module
VS
Require JS
•  Why Require JS
•  Using modules
•  Defining modules
•  Configuring Require JS
Using modules
main.js is the entry point of the app
The main HTML:
The main JS file:
Using modules
This function is called when all dependencies are loaded
If a required module calls define(), then this function is not
fired until its dependencies have been loaded
Required modules
References to
required modules
Require JS
•  Why Require JS
•  Using modules
•  Defining modules
•  Configuring Require JS
Module without dependencies Always one module per file
Public variables
Setup code
the simplest module can be a plain
collection of name/value pairs
module with initialization
The returned element can be any valid JS element
By convention I always return an object representing the
module
Module with dependencies
Dependency
definition
Dependent module reference
Dependent module
usage
This function is called when
zepto.js is loaded.
If zepto.js calls define(), then
this function is not fired until
also zepto’s dependencies
have loaded
Require JS under the hoods...
1.  loads each dependency as a script tag, using head.appendChild() and waits for all dependencies to
load
2.  computes the right order in which to call the functions that define the modules
3.  calls the module definition functions of each dependency in the right order
main.js
jQuery Backbone
SpinJS
MoviesCollection
MovieModel
MoviesView
1
2
3 4
5
67
Require JS
•  Why Require JS
•  Using modules
•  Defining modules
•  Configuring Require JS
Configuring Require JS
Require refers to a global configuration options
It allows developers to:
•  set the paths to all used frameworks in one place
•  use older frameworks as modules (shim)
•  define configuration params for the modules
•  etc.
Configuring Require JS
Shims for older
frameworks
paths to used frameworks
Dependent module
usage
Roadmap
•  Introduction
•  Require JS
•  Handlebars
•  Conclusions
Handlebars
•  Why Handlebars
•  Handlebars basics
•  Usage with Backbone and Require JS
Why Handlebars
We want to separate presentation from logic
TRANSLATE TO: we don’t want to put any HTML element into JavaScript code
separate logic from presentation
Imagine yourself trying to change how a movie should be rendered in
your app...
Handlebars
•  Why Handlebars
•  Handlebars basics
•  Usage with Backbone and Require JS
Example of template
A handlebars expression is
{{ something  }}
Escape values
Handlebars HTML-escapes all the values returned by an {{expression}}
If you don't want Handlebars to escape a value, use the "triple-stash“ à {{{ expression }}}
Populate your template
The recurrent process of obtaining a populated template is the following:
1.  create the template T with its placeholders {{ - }}
2.  compile the template into a JavaScript function t
3.  create a context CT containing the actual values for placeholders
4.  run the compiled template t(CT) to obtain the final HTML fragment
1. create the template
Templates are defined within a <script>	
  tag or in external files
2. compile the template
	
  
Handlebars.compile is used to compile a template
Compiling = obtaining a JS function representing the template
3. create a context for the template
	
  
A context is a JavaScript object used to populate a template
Here the keys of the object must match with the name of the placeholders to be populated
4. obtain the final HTML fragment
	
  
You have to execute a template with a context in order to get its corresponding HTML code
Expressions
	
  
The simplest expression is a simple identifier
This expression means "look up the username property in the current context"
Expressions with paths
Handlebars expressions can also be dot-separated paths
This expression means
"look up the user property in the current context,
then look up the username property of the user"
Helpers
Helpers are JavaScript functions that return HTML code
You should return a Handlebars SafeString if you don't want it to be escaped by default
Calling helpers
A Handlebars helper call is a simple identifier, followed by zero or more parameters
Each parameter is a Handlebars expression
es.
{{	
  test	
  user	
  }}	
  
In this case, test is the name of the Handlebars helper, and user is a parameter to the helper
Built-in helpers
It shifts the context for a section of a template
with
<div	
  class="entry“>	
  
<h1>{{title}}</h1>	
  
{{#with	
  author}}	
  
	
  <h2>By	
  {{firstName}}	
  {{lastName}}</h2>	
  
{{/with}}	
  
</div>	
  	
  
{	
  title:	
  "My	
  first	
  post!",	
  	
  
	
  	
  	
  author:	
  {	
  firstName:	
  “Ivano",	
  lastName:	
  “Malavolta"	
  }	
  
}	
  	
  
<div	
  class="entry“>	
  
<h1>My	
  first	
  post!</h1>	
  
<h2>By	
  Ivano	
  Malavolta</h2>	
  
</div>	
  	
  
Built-in helpers
To iterate over a list
each
Inside the block, you can use
this
to reference the element being iterated
<ul	
  class="people_list">	
  
	
   	
  {{#each	
  people}}	
  
	
   	
   	
  <li>{{this}}</li>	
  	
  
	
  {{/each}}	
  
	
  </ul>	
  	
  
{	
  people:	
  [	
  “Ivano",	
  “Andrea",	
  “Paolo"	
  ]	
  }	
  	
  
<ul	
  class="people_list">	
  	
  
	
  	
  <li>Ivano</li>	
  
	
  	
  <li>Andrea</li>	
  
	
  	
  <li>Paolo</li>	
  
</ul>	
  	
  
Built-in helpers
It renders the block if its argument is not equal to false,	
  undefined,	
  null,	
  []	
  
If / Else
The unless helper is the inverse of if
<div	
  class="entry“>	
  
<h1>{{title}}</h1>	
  
{{#if	
  author}}	
  
	
  <h2>By	
  {{firstName}}	
  {{lastName}}</h2>	
  
{{#else}}	
  
	
  <h2>Unknown	
  author</h1>	
  	
  
{{/if}}	
  
{	
  title:	
  "My	
  first	
  post!",	
  	
  
	
  	
  	
  author:	
  undefined	
  }	
  
}	
  	
  
<div	
  class="entry“>	
  
<h1>My	
  first	
  post!</h1>	
  
<h2>Unknown	
  author</h2>	
  
</div>	
  	
  
handlebars summary
Each Template can contain Expressions and Helpers operating on them
The main helpers are:
•  with	
  
•  each	
  
•  if	
  /	
  else	
  /unless	
  
You can define your own Helpers that operate on expressions, they return HTML code
A template can be (pre)-compiled and must be executed with a context in order to return the
final HTML fragment
Handlebars
•  Why Handlebars
•  Handlebars basics
•  Usage with Backbone and Require JS
Usage with Backbone and Require JS
Templates can be seen as special modules
So we can have the following:
•  a separate HTML file for each template
•  a Backbone view can have a dependency to each template
•  the template can be executed by using a JSON object of the Backbone model as context
Example
Dependency to template HTML file
It contains a string
Compiled template
Execution of the template
References
http://backbonejs.org
http://requirejs.org
http://handlebarsjs.com
https://github.com/iivanoo/cordovaboilerplate
Contact
Ivano Malavolta |
Gran Sasso Science Institute
iivanoo
ivano.malavolta@gssi.infn.it
www.ivanomalavolta.com

More Related Content

PDF
Backbone JS for mobile apps
PDF
[2015/2016] Local data storage for web-based mobile apps
PDF
PDF
[2015/2016] Backbone JS
PDF
Backbone.js
PDF
[2015/2016] Require JS and Handlebars JS
PDF
PDF
HTML5 & CSS3 refresher for mobile apps
Backbone JS for mobile apps
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Backbone JS
Backbone.js
[2015/2016] Require JS and Handlebars JS
HTML5 & CSS3 refresher for mobile apps

What's hot (20)

PDF
Fast mobile web apps
PDF
Backbone.js
PDF
Local storage in Web apps
PPTX
MVC & SQL_In_1_Hour
PDF
HTML5 and CSS3 refresher
PPTX
Getting Started with Angular JS
PPTX
Asp.Net MVC 5 in Arabic
PDF
Great Responsive-ability Web Design
PDF
D2W Branding Using jQuery ThemeRoller
PDF
Apache Cordova 4.x
PDF
[2015/2016] The REST architectural style
PPTX
Angular js 1.3 basic tutorial
PDF
Handlebars and Require.js
PDF
C# Advanced L09-HTML5+ASP
PDF
JavaScript
PPTX
Getting Started with jQuery
PDF
MVC 1.0 als alternative Webtechnologie
PPTX
Getting Started with Javascript
PPTX
Angular js for Beginnners
Fast mobile web apps
Backbone.js
Local storage in Web apps
MVC & SQL_In_1_Hour
HTML5 and CSS3 refresher
Getting Started with Angular JS
Asp.Net MVC 5 in Arabic
Great Responsive-ability Web Design
D2W Branding Using jQuery ThemeRoller
Apache Cordova 4.x
[2015/2016] The REST architectural style
Angular js 1.3 basic tutorial
Handlebars and Require.js
C# Advanced L09-HTML5+ASP
JavaScript
Getting Started with jQuery
MVC 1.0 als alternative Webtechnologie
Getting Started with Javascript
Angular js for Beginnners
Ad

Viewers also liked (20)

PDF
Mobile geolocation and mapping
PDF
The Mobile ecosystem, Context & Strategies
PDF
Backbone.js
PDF
Mobile Apps Development: Technological strategies and Monetization
PDF
Apache Cordova APIs version 4.3.0
PDF
Local data storage for mobile apps
PDF
PhoneGap: Accessing Device Capabilities
PDF
UI Design Patterns for Mobile Apps
PDF
PhoneGap: Local Storage
PDF
Mobile Applications Development - Lecture 0 - Spring 2013
PDF
[2015/2016] Modern development paradigms
PDF
Mobile Applications Development - Lecture 0
PDF
Design patterns for mobile apps
PDF
UI design for mobile apps
PDF
PhoneGap
PDF
Sitemaps & Wireframing
PDF
Javascript and jQuery for Mobile
PDF
[2015/2016] Introduction to software architecture
PDF
[2015/2016] AADL (Architecture Analysis and Design Language)
PDF
[2015/2016] HTML5 and CSS3 Refresher
Mobile geolocation and mapping
The Mobile ecosystem, Context & Strategies
Backbone.js
Mobile Apps Development: Technological strategies and Monetization
Apache Cordova APIs version 4.3.0
Local data storage for mobile apps
PhoneGap: Accessing Device Capabilities
UI Design Patterns for Mobile Apps
PhoneGap: Local Storage
Mobile Applications Development - Lecture 0 - Spring 2013
[2015/2016] Modern development paradigms
Mobile Applications Development - Lecture 0
Design patterns for mobile apps
UI design for mobile apps
PhoneGap
Sitemaps & Wireframing
Javascript and jQuery for Mobile
[2015/2016] Introduction to software architecture
[2015/2016] AADL (Architecture Analysis and Design Language)
[2015/2016] HTML5 and CSS3 Refresher
Ad

Similar to Handlebars & Require JS (20)

PDF
Handlebars.js
PDF
Introduction to javascript templating using handlebars.js
PDF
Dependency Management with RequireJS
PDF
Developing large scale JavaScript applications
PDF
Using RequireJS for Modular JavaScript Code
PDF
Require.JS
PDF
JavaScript Dependencies, Modules & Browserify
PDF
Modular JavaScript
PPTX
Angular JS in 2017
PDF
Javascript ui for rest services
PPTX
Welcome to React.pptx
PPTX
Javascript first-class citizenery
PDF
Resumen Backbone.js en Ingles
PPTX
JS Essence
PPTX
Java Script recruiting
PDF
PPTX
BackboneJS Training - Giving Backbone to your applications
PDF
Requirejs
PPT
Intro to-html-backbone
Handlebars.js
Introduction to javascript templating using handlebars.js
Dependency Management with RequireJS
Developing large scale JavaScript applications
Using RequireJS for Modular JavaScript Code
Require.JS
JavaScript Dependencies, Modules & Browserify
Modular JavaScript
Angular JS in 2017
Javascript ui for rest services
Welcome to React.pptx
Javascript first-class citizenery
Resumen Backbone.js en Ingles
JS Essence
Java Script recruiting
BackboneJS Training - Giving Backbone to your applications
Requirejs
Intro to-html-backbone

More from Ivano Malavolta (20)

PDF
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
PDF
Conducting Experiments on the Software Architecture of Robotic Systems (QRARS...
PDF
The H2020 experience
PDF
The Green Lab - Research cocktail @Vrije Universiteit Amsterdam (October 2020)
PDF
Software sustainability and Green IT
PDF
Navigation-aware and Personalized Prefetching of Network Requests in Android ...
PDF
How Maintainability Issues of Android Apps Evolve [ICSME 2018]
PDF
Collaborative Model-Driven Software Engineering: a Classification Framework a...
PDF
Experimenting on Mobile Apps Quality - a tale about Energy, Performance, and ...
PDF
Modeling objects interaction via UML sequence diagrams [Software Design] [Com...
PDF
Modeling behaviour via UML state machines [Software Design] [Computer Science...
PDF
Object-oriented design patterns in UML [Software Design] [Computer Science] [...
PDF
Structure modeling with UML [Software Design] [Computer Science] [Vrije Unive...
PDF
Requirements engineering with UML [Software Design] [Computer Science] [Vrije...
PDF
Modeling and abstraction, software development process [Software Design] [Com...
PDF
[2017/2018] Agile development
PDF
Reconstructing microservice-based architectures
PDF
[2017/2018] AADL - Architecture Analysis and Design Language
PDF
[2017/2018] Architectural languages
PDF
[2017/2018] Introduction to Software Architecture
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Conducting Experiments on the Software Architecture of Robotic Systems (QRARS...
The H2020 experience
The Green Lab - Research cocktail @Vrije Universiteit Amsterdam (October 2020)
Software sustainability and Green IT
Navigation-aware and Personalized Prefetching of Network Requests in Android ...
How Maintainability Issues of Android Apps Evolve [ICSME 2018]
Collaborative Model-Driven Software Engineering: a Classification Framework a...
Experimenting on Mobile Apps Quality - a tale about Energy, Performance, and ...
Modeling objects interaction via UML sequence diagrams [Software Design] [Com...
Modeling behaviour via UML state machines [Software Design] [Computer Science...
Object-oriented design patterns in UML [Software Design] [Computer Science] [...
Structure modeling with UML [Software Design] [Computer Science] [Vrije Unive...
Requirements engineering with UML [Software Design] [Computer Science] [Vrije...
Modeling and abstraction, software development process [Software Design] [Com...
[2017/2018] Agile development
Reconstructing microservice-based architectures
[2017/2018] AADL - Architecture Analysis and Design Language
[2017/2018] Architectural languages
[2017/2018] Introduction to Software Architecture

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
A Presentation on Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Modernizing your data center with Dell and AMD
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
A Presentation on Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
Modernizing your data center with Dell and AMD
The AUB Centre for AI in Media Proposal.docx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Mobile App Security Testing_ A Comprehensive Guide.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Approach and Philosophy of On baking technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf

Handlebars & Require JS

  • 1. Gran Sasso Science Institute Ivano Malavolta Handlebars & Require JS
  • 2. Roadmap •  Introduction •  Require JS •  Handlebars •  Conclusions
  • 3. Some technical advices from a real project...
  • 4. Some technical advices from a real project...
  • 5. Some technical advices from a real project...
  • 6. Some technical advices from a real project...
  • 7. Some technical advices from a real project...
  • 8. How you structure your applications MVC framework for giving structure File and module loader for code modularization Templating engine for separation of concerns
  • 9. How you structure your applications
  • 10. Roadmap •  Introduction •  Require JS •  Handlebars •  Conclusions
  • 11. Require JS •  Why Require JS •  Using modules •  Defining modules •  Configuring Require JS
  • 12. Why Require JS We are building apps, not websites We need well-specified and isolated JS files/modules Code complexity grows as the app gets bigger à we need some sort of #include/import/require   à ability to load nested dependencies
  • 13. What we want to avoid uncontrolled scripts poor control flow understanding
  • 14. RequireJS RequireJS is a JavaScript file and module loader Using a modular script loader like RequireJS will improve the modularity of your code à  speed in implementing changes à  better undestanding of the code Require JS allows modules to be loaded as fast as possible, even out of order, but evaluated in the correct dependency order Built on the Module Pattern JavaScript file and module loader
  • 15. The module pattern A JavaScript code module is some JavaScript code located in a registered location (e.g., a function) All of the code that runs inside the function lives in a closure, which provides •  privacy •  state throughout the lifetime of the module
  • 16. Module example Technically, it is simply a function that executes immediately
  • 17. Module VS script files A module is different from a traditional script file in that it defines a well-scoped object that avoids polluting the global namespace à its retained objects can be deleted by the GC It can explicitly list its dependencies and get a handle on those dependencies without needing to refer to global objects, but instead receive the dependencies as arguments to the function that defines the module VS
  • 18. Require JS •  Why Require JS •  Using modules •  Defining modules •  Configuring Require JS
  • 19. Using modules main.js is the entry point of the app The main HTML:
  • 20. The main JS file: Using modules This function is called when all dependencies are loaded If a required module calls define(), then this function is not fired until its dependencies have been loaded Required modules References to required modules
  • 21. Require JS •  Why Require JS •  Using modules •  Defining modules •  Configuring Require JS
  • 22. Module without dependencies Always one module per file Public variables Setup code the simplest module can be a plain collection of name/value pairs module with initialization The returned element can be any valid JS element By convention I always return an object representing the module
  • 23. Module with dependencies Dependency definition Dependent module reference Dependent module usage This function is called when zepto.js is loaded. If zepto.js calls define(), then this function is not fired until also zepto’s dependencies have loaded
  • 24. Require JS under the hoods... 1.  loads each dependency as a script tag, using head.appendChild() and waits for all dependencies to load 2.  computes the right order in which to call the functions that define the modules 3.  calls the module definition functions of each dependency in the right order main.js jQuery Backbone SpinJS MoviesCollection MovieModel MoviesView 1 2 3 4 5 67
  • 25. Require JS •  Why Require JS •  Using modules •  Defining modules •  Configuring Require JS
  • 26. Configuring Require JS Require refers to a global configuration options It allows developers to: •  set the paths to all used frameworks in one place •  use older frameworks as modules (shim) •  define configuration params for the modules •  etc.
  • 27. Configuring Require JS Shims for older frameworks paths to used frameworks Dependent module usage
  • 28. Roadmap •  Introduction •  Require JS •  Handlebars •  Conclusions
  • 29. Handlebars •  Why Handlebars •  Handlebars basics •  Usage with Backbone and Require JS
  • 30. Why Handlebars We want to separate presentation from logic TRANSLATE TO: we don’t want to put any HTML element into JavaScript code separate logic from presentation Imagine yourself trying to change how a movie should be rendered in your app...
  • 31. Handlebars •  Why Handlebars •  Handlebars basics •  Usage with Backbone and Require JS
  • 32. Example of template A handlebars expression is {{ something  }}
  • 33. Escape values Handlebars HTML-escapes all the values returned by an {{expression}} If you don't want Handlebars to escape a value, use the "triple-stash“ à {{{ expression }}}
  • 34. Populate your template The recurrent process of obtaining a populated template is the following: 1.  create the template T with its placeholders {{ - }} 2.  compile the template into a JavaScript function t 3.  create a context CT containing the actual values for placeholders 4.  run the compiled template t(CT) to obtain the final HTML fragment
  • 35. 1. create the template Templates are defined within a <script>  tag or in external files
  • 36. 2. compile the template   Handlebars.compile is used to compile a template Compiling = obtaining a JS function representing the template
  • 37. 3. create a context for the template   A context is a JavaScript object used to populate a template Here the keys of the object must match with the name of the placeholders to be populated
  • 38. 4. obtain the final HTML fragment   You have to execute a template with a context in order to get its corresponding HTML code
  • 39. Expressions   The simplest expression is a simple identifier This expression means "look up the username property in the current context"
  • 40. Expressions with paths Handlebars expressions can also be dot-separated paths This expression means "look up the user property in the current context, then look up the username property of the user"
  • 41. Helpers Helpers are JavaScript functions that return HTML code You should return a Handlebars SafeString if you don't want it to be escaped by default
  • 42. Calling helpers A Handlebars helper call is a simple identifier, followed by zero or more parameters Each parameter is a Handlebars expression es. {{  test  user  }}   In this case, test is the name of the Handlebars helper, and user is a parameter to the helper
  • 43. Built-in helpers It shifts the context for a section of a template with <div  class="entry“>   <h1>{{title}}</h1>   {{#with  author}}    <h2>By  {{firstName}}  {{lastName}}</h2>   {{/with}}   </div>     {  title:  "My  first  post!",          author:  {  firstName:  “Ivano",  lastName:  “Malavolta"  }   }     <div  class="entry“>   <h1>My  first  post!</h1>   <h2>By  Ivano  Malavolta</h2>   </div>    
  • 44. Built-in helpers To iterate over a list each Inside the block, you can use this to reference the element being iterated <ul  class="people_list">      {{#each  people}}        <li>{{this}}</li>      {{/each}}    </ul>     {  people:  [  “Ivano",  “Andrea",  “Paolo"  ]  }     <ul  class="people_list">        <li>Ivano</li>      <li>Andrea</li>      <li>Paolo</li>   </ul>    
  • 45. Built-in helpers It renders the block if its argument is not equal to false,  undefined,  null,  []   If / Else The unless helper is the inverse of if <div  class="entry“>   <h1>{{title}}</h1>   {{#if  author}}    <h2>By  {{firstName}}  {{lastName}}</h2>   {{#else}}    <h2>Unknown  author</h1>     {{/if}}   {  title:  "My  first  post!",          author:  undefined  }   }     <div  class="entry“>   <h1>My  first  post!</h1>   <h2>Unknown  author</h2>   </div>    
  • 46. handlebars summary Each Template can contain Expressions and Helpers operating on them The main helpers are: •  with   •  each   •  if  /  else  /unless   You can define your own Helpers that operate on expressions, they return HTML code A template can be (pre)-compiled and must be executed with a context in order to return the final HTML fragment
  • 47. Handlebars •  Why Handlebars •  Handlebars basics •  Usage with Backbone and Require JS
  • 48. Usage with Backbone and Require JS Templates can be seen as special modules So we can have the following: •  a separate HTML file for each template •  a Backbone view can have a dependency to each template •  the template can be executed by using a JSON object of the Backbone model as context
  • 49. Example Dependency to template HTML file It contains a string Compiled template Execution of the template
  • 51. Contact Ivano Malavolta | Gran Sasso Science Institute iivanoo ivano.malavolta@gssi.infn.it www.ivanomalavolta.com