-
chad.lung =at= gmail.com
Please do not email me seeking help with your project or code
Follow via RSS or ATOM
Search
-
March 2012 M T W T F S S « Feb 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Categories
- ActionScript (33)
- Adobe (18)
- AIR (3)
- AJAX (11)
- Amazon Web Services (3)
- Android (12)
- Apache Abdera (9)
- Apache Mina (2)
- Apache Tomcat (7)
- App Engine (6)
- ASP.NET (9)
- Atom Hopper (9)
- BlazeDS (1)
- C# (14)
- CherryPy (1)
- Comet (3)
- CouchDB (2)
- Design Patterns (3)
- Dojo (1)
- Erlang (1)
- Flash (25)
- Flex (31)
- Go (1)
- GWT (20)
- HTML5 (23)
- iPhone (1)
- Java (63)
- JavaFX (1)
- JavaScript (42)
- JSON Feed Server (1)
- Mobile (17)
- MongoDB (6)
- Netbeans (18)
- Node.js (18)
- Online Games (11)
- Open Source (61)
- Perl (1)
- PHP (23)
- Python (9)
- Rails (3)
- REST (4)
- Reviews (6)
- Ruby (11)
- Silverlight (3)
- Sinatra (3)
- Socket Server (19)
- Ubuntu (33)
- Uncategorized (5)
- Union Framework (8)
- Vaadin (2)
- WebORB (3)
Using the new JQuery Templates with AJAX and PHP
Posted on April 4, 2011 by Chad Lung
The JQuery team and Microsoft have been working on adding templates to JQuery. Today I’ll show you how to use the JQuery Templates and load data from PHP via AJAX.
Note: This was written against beta 1 of the JQuery Templates, things most likely will change and this code might need to be modified to continue working in future releases.
The PHP page is very easy and simply writes back some JSON.
File name: callajax.php
<?php echo json_encode(array(array('manufacturer' => 'Apple', 'mobileDevice' => 'iPad2'), array('manufacturer' => 'Motorola', 'mobileDevice' => 'Xoom'), array('manufacturer' => 'Samsung', 'mobileDevice' => 'Galaxy Tab'))); ?>
The HTML markup I’m going to use will look like this:
<script id="mobileDeviceTemplate" type="text/x-jquery-tmpl"> <li> <strong>${manufacturer}:</strong> ${mobileDevice} </li> </script>
This will get rendered into this unordered list:
<ul id="ajaxResult"></ul>
When the Ajax is triggered it calls the PHP page and the onSuccess function is called to render the template to the browser:
$("#mobileDeviceTemplate").tmpl(data).appendTo("#ajaxResult");
The full code listing for the index.html page looks like this:
<!DOCTYPE html> <html lang="en"> <head> <title>JQuery Templates</title> <script src="/img/spacer.gif">Here are the results:
This entry was posted in AJAX, JavaScript, Open Source, PHP. Bookmark the permalink.
3 Responses to Using the new JQuery Templates with AJAX and PHP
Good basic description of jquery templates. Would like to see more of this as well as using the templates with jsonp data
Thanks!
yes…its quietly simple tutorial but it’s usefull.. if i have to implement jQuery template in real world apps then my client side script with full of large of bunch of html template..would that cost roundtrip performance?? ..thanks
@Noel,
You just need to determine how much data your pushing to the client. Typically this won’t be anything to be concerned about. Nowadays most people/places have good amounts of bandwidth available to them as they are streaming video and audio, etc. Depends on what your target devices are, ie: mobile? desktop? With mobile you might want to take into consideration the slower 3g networks, etc.
There is always compression you could tinker with on the web server.
Chad