Dr. Charles Severance www.wa4e.com Using Handlebars Dr. Charles Severance www.wa4e.com http://www.wa4e.com/code/handlebars.zip.

Slides:



Advertisements
Similar presentations
Introduction to PHP Dr. Charles Severance
Advertisements

PHP Functions / Modularity
IS 360 Course Introduction. Slide 2 What you will Learn (1) The role of Web servers and clients How to create HTML, XHTML, and HTML 5 pages suitable for.
Creating WordPress Websites. Creating a site on your computer Local server Local WordPress installation Setting Up Dreamweaver.
Multiple Tiers in Action
Getting PHP Hosting Dr. Charles Severance. What We Need We want to be able to put up our application on the web so anyone can access our URL (and so we.
INTRO TO MAKING A WEBSITE Mark Zhang.  HTML  CSS  Javascript  PHP  MySQL  …That’s a lot of stuff!
Forms and PHP Dr. Charles Severance
Cookies, Sessions, and Authentication Dr. Charles Severance
JavaScript Teppo Räisänen LIIKE/OAMK HTML, CSS, JavaScript HTML defines the structure CSS defines the layout JavaScript is used for scripting It.
PHP Arrays Dr. Charles Severance
Introduction to Dynamic Web Content Dr. Charles Severance
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
PHP Functions Dr. Charles Severance
Using jQuery / APIs / JSON Dr. Charles Severance
JavaScript Dr. Charles Severance
Accessing MySQL Using PDO Charles Severance
Forms and PHP Dr. Charles Severance
PHP Arrays Dr. Charles Severance
PHP Functions / Modularity Dr. Charles Severance
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Introduction to AJAX MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/4/2016.
Using jQuery Dr. Charles Severance
Using Handlebars Dr. Charles Severance
Introduction to Dynamic Web Content Dr. Charles Severance
Introduction to Dynamic Web Content Chapter 1 Dr. Charles Severance To be used in assocition with the book: PHP, MySql, and JavaScript by Robin Nixon.
C.R.U.D. Charles Severance
Form Processing in PHP Dr. Charles Severance
Introduction to Dynamic Web Content
PHP Arrays Dr. Charles Severance
Our Technologies Dr. Charles Severance
Forms and PHP.
HTML Charles Severance
JavaScript Dr. Charles Severance
PHP Functions / Modularity
Transactions Dr. Charles Severance
Cascading Style Sheets
Getting PHP Hosting Dr. Charles Severance
Using JSON Dr. Charles Severance
Redirect, Routing, and Authentication
Introduction to PHP Dr. Charles Severance
Creating UI elements with Handlebars
Loops and Iteration Chapter 5 Python for Everybody
EXCEPTION HANDLING IN SERVER CLIENT PROGRAMMING
Introduction to PHP Dr. Charles Severance
HTTP Parameters and Arrays
Functions Chapter 4 Python for Everybody
Accessing MySQL Using PDO
PHP Arrays Dr. Charles Severance
Super Global Arrays Forms and PHP "Superglobal" arrays (6.4.1)
Super Global Arrays Forms and PHP "Superglobal" arrays (6.4.1)
CMPE 280 Web UI Design and Development November 7 Class Meeting
Object Oriented Programming in JavaScript
>> PHP: Form Processing
Expressions and Control Flow in PHP
Web Programming– UFCFB Lecture 22
Tuples Chapter 10 Python for Everybody
PHP Overview PHP: Hypertext Preprocessor Server-Side Scripting
Intro to PHP at Winthrop
PHP Namespaces (in progress)
Web Browser server client 3-Tier Architecture Apache web server PHP
Forms and PHP.
Introduction to Dynamic Web Content
Using jQuery Dr. Charles Severance
Secure Web Programming
Charles Severance Single Table SQL.
Introduction to Dynamic Web Content
HTML Tags and Structure
Data Modelling Many to Many
Model View Controller (MVC)
Presentation transcript:

Dr. Charles Severance www.wa4e.com Using Handlebars Dr. Charles Severance www.wa4e.com http://www.wa4e.com/code/handlebars.zip

Rendering in the Browser Applications are starting to use JSON “back end” services and construct the View HTML in JavaScript www.backbonejs.org www.emberjs.com www.angular.net www.reactjs.net ... The only correct way to write JavaScript is whatever you were not doing last week. @ThePracticalDev

Time DOM Apache MySql PHP http://www.wa4e.com/code/rrc/ Browser Web Server Database Server DOM Send Request Apache MySql Parse Response PHP JavaScript JQuery http://www.wa4e.com/code/rrc/

Model-View-Controller A model that defines the elements of a web application and how they interact View – Produce output Model – Handle data Controller – Orchestration / Routing https://en.wikipedia.org/wiki/Model-view-controller

guess.php Model Context Controller View <?php $oldguess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $oldguess = $_POST['guess'] + 0; if ( $oldguess == 42 ) { $message = "Great job!"; } else if ( $oldguess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> <html> <head> <title>A Guessing game</title> </head> <body style="font-family: sans-serif;"> <p>Guessing game...</p> if ( $message !== false ) { echo("<p>$message</p>\n"); <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" size="40" value="<?= htmlentities($oldguess) ?>"/></p> <input type="submit"/> </form> </body> Model Context Controller View guess.php

Model Time DOM Apache MySql PHP Controller Context View MVC in PHP Browser Web Server Database Server DOM Send Request Apache MySql Parse Response PHP Model Controller Context P DO JavaScript View MVC in PHP

handlebars.js Templates with curly braces Adapted from Django / AppEngine Hot spots accept data from the context <div class="entry"> <h1>{{title}}</h1> <div class="body"> {{body}} </div> <div class="entry"> <h1>Equations</h1> <div class="body"> guess < 42 </div> Context + =

<div id="stuff"><img src="spinner.gif"></div> <script id="entry-template" type="text/x-handlebars-template"> <div class="entry"> <h1>{{title}}</h1> <div class="body"> {{body}} </div> </script> <script> var raw_tempate = $("#entry-template").html(); var template = Handlebars.compile(raw_template); var context = {title: "Equations", body: "guess < 42"}; var rendered = template(context); alert('Check out the sweet spinner..:)'); $('#stuff').html(rendered); hand-01.htm

Model Time DOM Apache MySql PHP Controller Context View MVC in PHP Browser Web Server Database Server DOM Send Request Apache MySql Parse Response PHP Model Controller Context P DO JavaScript View MVC in PHP

Model Time DOM Apache MySql PHP Controller Context Context View Browser Web Server Database Server DOM Send Request Apache MySql Parse Response PHP Model Controller JavaScript Context P DO Context View View in JavaScript

hand-02.htm <?php header('Content-Type: ...'); $stuff = array( <div id="stuff"><img src="spinner.gif"></div> <script id="entry-template" type="text/x-handlebars-template"> <div class="entry"> <h1>{{title}}</h1> <div class="body"> {{body}} </div> </script> <script> var raw_tempate = $("#entry-template").html(); var template = Handlebars.compile(raw_template); $.getJSON('hand-02-json.php', function (context) { var rendered = template(context); $('#stuff').replaceWith(rendered); }); hand-02.htm <?php header('Content-Type: ...'); $stuff = array( 'title' => 'Mathematics', 'body' => 'guess > 42'); echo(json_encode($stuff)); { "title":"Mathematics", "body":"guess > 42" }

Handlebars Application

index.php res-handlebars <script id="list-template" type="text/x-handlebars-template"> {{#if profiles.length}} <p><table border="1"> <tr><th>Name</th><th>Headline</th> {{#if loggedin}}<th>Action</th>{{/if}}</tr> {{#each profiles}} <tr><td><a href="view.php?profile_id={{profile_id}}"> {{first_name}} {{last_name}}</a> </td><td>{{headline}}</td> {{#if ../loggedin}} <td> <a href="form.php?profile_id={{profile_id}}">Edit</a> <a href="delete.php?profile_id={{profile_id}}">Delete</a> </td> {{/if}} </tr> {{/each}} </table></p> </script> index.php res-handlebars

index.php <div id="list-area"><img src="spinner.gif"></div> .... <script> $(document).ready(function(){ $.getJSON('profiles.php', function(profiles) { window.console && console.log(profiles); var source = $("#list-template").html(); var template = Handlebars.compile(source); var context = {}; context.loggedin = <?= isset($_SESSION['user_id']) ? 'true' : 'false' ?>; context.profiles = profiles; $('#list-area').replaceWith(template(context)); }).fail( function() { alert('getJSON fail'); } ); }); </script>

profiles.php <?php // This script works even if you are not logged in require_once 'pdo.php'; header("Content-type: application/json; charset=utf-8"); $stmt = $pdo->query('SELECT * FROM Profile'); $profiles = $stmt->fetchAll(PDO::FETCH_ASSOC); echo(json_encode($profiles, JSON_PRETTY_PRINT));

profiles.php [ { "profile_id": "5", "user_id": "1", "first_name": "Chuck", "last_name": "Severance", "email": "csev@example.com", "headline": "Python Rulez", "summary": "Python", "updated_at": "2016-04-04 21:15:27" }, "profile_id": "6", "first_name": "Colleen", "last_name": "van Lent", "email": "colleen@example.com", "headline": "Responsive Design Rulez", "summary": "HTML5!", "updated_at": "2016-04-04 21:16:12" } ] profiles.php

Model Model Model Time DOM Apache MySql Controller Context View Browser Web Server Database Server Send Request DOM Apache MySql Parse Response JavaScript Model Controller Model P DO Context Model View Single Page Application - Angular

Summary With JavaScript ES6 on the way and significant browser improvements on the horizon, it is likely that the “best practice” both on the server and the client will continue to evolve. It will continue to be important to understand how web applications work “all the way down” so you can work with these new innovations. The only correct way to write JavaScript is whatever you were not doing last week. @ThePracticalDev

Acknowledgements / Contributions These slides are Copyright 2010- Charles R. Severance (www.dr-chuck.com) as part of www.wa4e.com and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Continue new Contributors and Translators here