Creating UI elements with Handlebars

Slides:



Advertisements
Similar presentations
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
Advertisements

AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
XML Processing SoftUni Team Database Applications Technical Trainers
Software Technologies
Version Control Systems
Helpers, Data Validation
Auto Mapping Objects SoftUni Team Database Applications
Databases basics Course Introduction SoftUni Team Databases basics
C# Basic Syntax, Visual Studio, Console Input / Output
ASP.NET Essentials SoftUni Team ASP.NET MVC Introduction
C# Basic Syntax, Visual Studio, Console Input / Output
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
Thymeleaf and Spring Controllers
WordPress Introduction
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
Build a WordPress Site A Real Life Example: Create a Fully Functional WP Business Web Site from Scratch Building a WP Site SoftUni Team Technical Trainers.
Introduction to Entity Framework
Source Control Systems
Mocking tools for easier unit testing
Parsing JSON JSON.NET, LINQ-to-JSON
JavaScript Applications
State Management Cookies, Sessions SoftUni Team State Management
Browser Routing Design Patterns in JS
Software Technologies
Parsing XML XDocument and LINQ
Repeating Code Multiple Times
Unit Testing with Mocha
Databases advanced Course Introduction SoftUni Team Databases advanced
MVC Architecture. Routing
Creating React Components with JSX and Babel
Caching Data in ASP.NET MVC
Fast String Manipulation
Array and List Algorithms
Modules, Babel, RequireJS, Other JavaScript Module Systems
The Right Way Control Flow
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
MVC Architecture, Symfony Framework for PHP Web Apps
Processing Variable-Length Sequences of Elements
AJAX and jQuery AJAX AJAX Concepts, XMLHttpRequest, jQuery AJAX: $.ajax(), $.get(), $.post() jQuery AJAX XMLHttpRequest SoftUni Team Technical Trainers.
ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Best Practices and Architecture
C# Web Development Basics
Design & Module Development
Magento Basics part 2 Modules & Themes Stenik Group Ltd. Magento
WordPress Plugins Popular WP Plugins: Sliders, Forms, Contacts, SEO, Forum, Photo Gallery, e-Commerce WordPress Plugins SoftUni Team Technical Trainers.
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Exporting and Importing Data
Making big SPA applications
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Course Overview, Trainers, Evaluation
Exporting and Importing Data
Scheduled Tasks and Web Socket
JavaScript Fundamentals
Introduction to TypeScript & Angular
CSS Transitions and Animations
Train the Trainers Course
Software Quality Assurance
Version Control Systems
JavaScript Frameworks & AngularJS
First-Class Functions
JavaScript: ExpressJS Overview
CSS Transitions and Animations
Iterators and Generators
Presentation transcript:

Creating UI elements with Handlebars Templating Creating UI elements with Handlebars Templating SoftUni Team Technical Trainers Software University http://softuni.bg

Table of Contents Templating Concepts Simple Templating Templating Engines Handlebars Overview Examples

Have a Question? sli.do #JSAPPS

Templating Concepts Definition and Uses

What is Templating? Templating Engine Templates allow similar content to be replicated in a web page, without repeating the corresponding markup everywhere <div> <span> <button> HTML Ivan, Maria, Jordan, … Dynamic Content Templating Engine

Templating Concepts The static parts of a webpage are stored as templates The dynamic content is kept separately (e.g. in a database) A templating engine combines the two Benefits: Productivity – avoid writing the same markup over and over Save bandwidth – send the HTML once, fill in any content Composability – a single element can be used on multiple pages

Examples Display articles in a blog Template Templating Engine Content

Examples Templating Engine Display articles in a blog Display a gallery of photos Template Templating Engine Content

Examples Templating Engine Display articles in a blog Display a gallery of photos Visualize user profiles Template Templating Engine Content

Examples Templating Engine Display articles in a blog Display a gallery of photos Visualize user profiles Show items in a catalog Template Templating Engine Content

Simple HTML Templating Live Demo Simple HTML Templating

Overview of Popular JS Libraries Templating Engines Overview of Popular JS Libraries

Templating Engines Pug Vue jQuery Templates Underscore Templates Mustache Handlebars

Templating with Handlebars Syntax and Examples

Overview Based on the Mustache specification Adds helper functions and nested context paths Uses double curly brace notation {{ }} <div class="entry"> <h1>{{ title }}</h1> <div class="body"> {{ body }} </div> <div class="entry"> <h1>My New Post</h1> <div class="body"> This is my first post! </div>

Example Compile the template from a string Populate with data <script id="entry-template" type="text/x-handlebars-template"> <div class="entry"> <h1>{{title}}</h1> <div class="body">{{body}}</div> </div> </script> let source = $("#entry-template").html(); let template = Handlebars.compile(source); let context = { title: "My New Post", body: "This is my first post!" }; let html = template(context); Compile the template from a string Populate with data

npm install --save handlebars node_modules/handlebars/dist/ Installation Download Handlebars using WebStorm's terminal Or download from handlebarsjs.com Browser builds will be located in Use handlebars from an online CDN: npm install --save handlebars node_modules/handlebars/dist/ https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.min.js Note: It's best if your project has a package.json file

Hello Handlebars <!DOCTYPE html> <head> <meta charset="UTF-8"> <title>Hello Handlebars</title> <!-- Include Handlebars distribution --> </head> <body> <div id="app"></div> <script> let template = Handlebars.compile('<h1>Hello {{name}}</h1>'); let container = document.getElementById('app'); container.innerHTML = template({ name: 'Handlebars' }); </script> </body> </html>

Expressions You can place your templates in a script element Use type text/x-handlebars-template Give the element an ID for easier use Anything inside double curly braces {{ }} will be evaluated Set the context by executing the template with an object Named identifiers will be looked up in the object's properties Handlebars supports conditional statements and other helpers

Simple Identifiers <script id="contact-template" type="text/x-handlebars-template"> <article> <div class="title"> {{name}} <button>ℹ</button> </div> <div class="info"> <span>&phone; {{phone}}</span> <span>✉ {{email}}</span> </article> </script>

Compilation and Execution Once loaded, a template must be compiled Compiled templates are functions, that can be executed with whatever variables we need let source = $("#contact-template").html(); let template = Handlebars.compile(source); let context = { name: 'Ivan Ivanov', phone: '0888 123 456', email: 'i.ivanov@gmail.com' }; let html = template(context);

For-Loops A template can be repeated for every entry in an array let context = { contacts: [ { name: 'Ivan Ivanov', email: 'i.ivanov@gmail.com'}, { name: 'Maria Petrova', email: 'mar4eto@abv.bg'}, { name: 'Jordan Kirov', email: 'jordk@gmail.com'} ]}; <ul id="contacts"> {{#each contacts}} <li>{{name}}: {{email}}</li> {{/each}} </ul> The expression inside the loop uses each entry as context

Conditional Statements Variable to check for truthiness {{#if sunny}} The sky is clear {{else}} The sky is overcast {{/if}} <ul id="contacts"> {{#each contacts}} <li>{{name}}: {{email}}</li> {{else}} <i>(empty)<i> {{/each}} </ul> Will be shown if the array is empty

Partials Partials are templates that can be inserted into other templates let source = $("#contact-template").html(); Handlebars.registerPartial('contact', source); Partial name Template as string <div id="contacts"> {{#each contacts}} {{> contact}} {{else}} <i>(empty)<i> {{/each}} </div> Partials are globally accessible

HTML Escaping By default, any strings that are evaluated will be HTML-escaped To prevent this, use the "triple-stash" title: "All about <p> Tags" body: "<p>This is a post about <p> tags</p>" <div class="entry"> <h1>All About <p> Tags</h1> <div class="body"> <p> This is a post about <p> tags </p> </div> <div class="entry"> <h1>{{title}}</h1> <div class="body"> {{{body}}} </div>

Templating with Handlebars Live Demo Templating with Handlebars

Live Exercises in Class (Lab) Practice: Templating Live Exercises in Class (Lab)

Summary Templates speed up and simplify the development process Handlebars offers effective templates and simple helper functions <div class="entry"> <h1>{{ title }}</h1> <div class="body"> {{ body }} </div> <div class="entry"> <h1>My New Post</h1> <div class="body"> This is my first post! </div>

Templating https://softuni.bg/courses/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.