You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

octemplate.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import $ from 'jquery'
  2. import escapeHTML from 'escape-html'
  3. /**
  4. * jQuery plugin for micro templates
  5. *
  6. * Strings are automatically escaped, but that can be disabled by setting
  7. * escapeFunction to null.
  8. *
  9. * Usage examples:
  10. *
  11. * var htmlStr = '<p>Bake, uncovered, until the {greasystuff} is melted and the {pasta} is heated through, about {min} minutes.</p>'
  12. * $(htmlStr).octemplate({greasystuff: 'cheese', pasta: 'macaroni', min: 10});
  13. *
  14. * var htmlStr = '<p>Welcome back {user}</p>';
  15. * $(htmlStr).octemplate({user: 'John Q. Public'}, {escapeFunction: null});
  16. *
  17. * Be aware that the target string must be wrapped in an HTML element for the
  18. * plugin to work. The following won't work:
  19. *
  20. * var textStr = 'Welcome back {user}';
  21. * $(textStr).octemplate({user: 'John Q. Public'});
  22. *
  23. * For anything larger than one-liners, you can use a simple $.get() ajax
  24. * request to get the template, or you can embed them it the page using the
  25. * text/template type:
  26. *
  27. * <script id="contactListItemTemplate" type="text/template">
  28. * <tr class="contact" data-id="{id}">
  29. * <td class="name">
  30. * <input type="checkbox" name="id" value="{id}" /><span class="nametext">{name}</span>
  31. * </td>
  32. * <td class="email">
  33. * <a href="mailto:{email}">{email}</a>
  34. * </td>
  35. * <td class="phone">{phone}</td>
  36. * </tr>
  37. * </script>
  38. *
  39. * var $tmpl = $('#contactListItemTemplate');
  40. * var contacts = // fetched in some ajax call
  41. *
  42. * $.each(contacts, function(idx, contact) {
  43. * $contactList.append(
  44. * $tmpl.octemplate({
  45. * id: contact.getId(),
  46. * name: contact.getDisplayName(),
  47. * email: contact.getPreferredEmail(),
  48. * phone: contact.getPreferredPhone(),
  49. * });
  50. * );
  51. * });
  52. */
  53. /**
  54. * Object Template
  55. * Inspired by micro templating done by e.g. underscore.js
  56. */
  57. const Template = {
  58. init: function(vars, options, elem) {
  59. // Mix in the passed in options with the default options
  60. this.vars = vars
  61. this.options = $.extend({}, this.options, options)
  62. this.elem = elem
  63. var self = this
  64. if (typeof this.options.escapeFunction === 'function') {
  65. var keys = Object.keys(this.vars)
  66. for (var key = 0; key < keys.length; key++) {
  67. if (typeof this.vars[keys[key]] === 'string') {
  68. this.vars[keys[key]] = self.options.escapeFunction(this.vars[keys[key]])
  69. }
  70. }
  71. }
  72. var _html = this._build(this.vars)
  73. return $(_html)
  74. },
  75. // From stackoverflow.com/questions/1408289/best-way-to-do-variable-interpolation-in-javascript
  76. _build: function(o) {
  77. var data = this.elem.attr('type') === 'text/template' ? this.elem.html() : this.elem.get(0).outerHTML
  78. try {
  79. return data.replace(/{([^{}]*)}/g,
  80. function(a, b) {
  81. var r = o[b]
  82. return typeof r === 'string' || typeof r === 'number' ? r : a
  83. }
  84. )
  85. } catch (e) {
  86. console.error(e, 'data:', data)
  87. }
  88. },
  89. options: {
  90. escapeFunction: escapeHTML
  91. }
  92. }
  93. $.fn.octemplate = function(vars, options) {
  94. vars = vars || {}
  95. if (this.length) {
  96. var _template = Object.create(Template)
  97. return _template.init(vars, options, this)
  98. }
  99. }