Member Login

Username:
Password:
Remember:

jquery.templates.js | 1.79 KB | alec | November 23rd, 2009 | 0 Downloads | 141 Views
File Name: jquery.templates.js
File Size: 1.79 KB
Uploaded By:  alec
MD5 Hash:
SHA1 Hash:
Downloads: 0
Page Views: 141
Date Uploaded: Nov 23rd, 2009 at 6:10:30 pm
Last Downloaded:  File has not been downloaded
File Status:  Public File
  1. /*!
  2.  * jQuery JSON Templater Plugin
  3.  *
  4.  * Copyright (c) 2009 Ramblingwood
  5.  * Dual licensed under the MIT and GPL licenses.
  6.  * See MIT-LICENSE.txt and GPL-LICENSE.txt
  7.  *
  8.  */
  9. jQuery.templates = {};
  10. jQuery.templates.removeArrBlanks = function (inarr) {
  11. for(var i in inarr) {
  12. if(inarr[i] === '')
  13. inarr.splice(i, 1);
  14. }
  15. return inarr;
  16. };
  17. jQuery.templates.engine = function (text,data) {
  18. pieces = text.split(/{{([^}]+)}}/);
  19. for (var i in pieces) {
  20. if (i%2 == 1) {
  21. if(pieces[i].indexOf('[') > -1) {
  22. var hit = jQuery.templates.removeArrBlanks(pieces[i].split(/[([^]]+)]/));
  23. var evalstring = 'data['+(typeof(hit[0]) == 'number' ? hit[0] : ("'"+hit[0]+"'"))+']';
  24. for(var x = 1; x < hit.length; x++) {
  25. var evalstring = evalstring +'['+hit[x]+']';
  26. }
  27. pieces[i] = eval(evalstring);
  28. }
  29. else if ( typeof(data[pieces[i]]) != 'undefined' ) {
  30. pieces[i] = data[pieces[i]];
  31. }
  32. }
  33. }
  34. return pieces.join('');
  35. };
  36. jQuery.fn.extend({
  37. jsonRender : function (data, returnArr) {
  38. var engine = jQuery.templates.engine;
  39. if(!data[0])
  40. return '';
  41. results = new Array();
  42. $.each(this, function (key,val) {
  43. results[key] = new Array();
  44. for(var k in data) {
  45. results[key].push(engine($(val).html(), data[k]));
  46. };
  47. });
  48. if(this.length == 1 && returnArr == true)
  49. return results[0];
  50. else if(returnArr == true)
  51. return results;
  52. return results[0].join('');
  53. },
  54. render : function (data, returnArr) {
  55. var engine = jQuery.templates.engine;
  56. results = new Array();
  57. if(this.length > 1) {
  58. $.each(this, function () {
  59. results.push(engine($(this).html(), data));
  60. });
  61. return results;
  62. }
  63. else {
  64. if(typeof(returnArr) == 'undefined' || returnArr == false)
  65. return engine($(this).html(), data);
  66. results[0] = engine($(this).html(), data);
  67. return results;
  68. }
  69. }
  70. });
 
By downloading this file, you agree to our Terms of Service and Privacy Policy. If this file violates our terms of service, please report it. Report file