選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

sammy.template.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. (function (factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. define(['jquery', 'sammy'], factory);
  4. } else {
  5. (window.Sammy = window.Sammy || {}).Template = factory(window.jQuery, window.Sammy);
  6. }
  7. }(function ($, Sammy) {
  8. // Simple JavaScript Templating
  9. // John Resig - http://ejohn.org/ - MIT Licensed
  10. // adapted from: http://ejohn.org/blog/javascript-micro-templating/
  11. // originally $.srender by Greg Borenstein http://ideasfordozens.com in Feb 2009
  12. // modified for Sammy by Aaron Quint for caching templates by name
  13. var srender_cache = {};
  14. var srender = function(name, template, data, options) {
  15. var fn, escaped_string;
  16. // target is an optional element; if provided, the result will be inserted into it
  17. // otherwise the result will simply be returned to the caller
  18. if (srender_cache[name]) {
  19. fn = srender_cache[name];
  20. } else {
  21. if (typeof template == 'undefined') {
  22. // was a cache check, return false
  23. return false;
  24. }
  25. // If options escape_html is false, dont escape the contents by default
  26. if (options && options.escape_html === false) {
  27. escaped_string = "\",$1,\"";
  28. } else {
  29. escaped_string = "\",h($1),\"";
  30. }
  31. // Generate a reusable function that will serve as a template
  32. // generator (and which will be cached).
  33. fn = srender_cache[name] = new Function("obj",
  34. "var ___$$$___=[],print=function(){___$$$___.push.apply(___$$$___,arguments);};" +
  35. // Introduce the data as local variables using with(){}
  36. "with(obj){___$$$___.push(\"" +
  37. // Convert the template into pure JavaScript
  38. String(template)
  39. .replace(/[\r\t\n]/g, " ")
  40. .replace(/\"/g, '\\"')
  41. .split("<%").join("\t")
  42. .replace(/((^|%>)[^\t]*)/g, "$1\r")
  43. .replace(/\t=(.*?)%>/g, escaped_string)
  44. .replace(/\t!(.*?)%>/g, "\",$1,\"")
  45. .split("\t").join("\");")
  46. .split("%>").join("___$$$___.push(\"")
  47. .split("\r").join("")
  48. + "\");}return ___$$$___.join('');");
  49. }
  50. if (typeof data != 'undefined') {
  51. return fn(data);
  52. } else {
  53. return fn;
  54. }
  55. };
  56. // `Sammy.Template` is a simple plugin that provides a way to create
  57. // and render client side templates. The rendering code is based on John Resig's
  58. // quick templates and Greg Borenstien's srender plugin.
  59. // This is also a great template/boilerplate for Sammy plugins.
  60. //
  61. // Templates use `<% %>` tags to denote embedded javascript.
  62. //
  63. // ### Examples
  64. //
  65. // Here is an example template (user.template):
  66. //
  67. // // user.template
  68. // <div class="user">
  69. // <div class="user-name"><%= user.name %></div>
  70. // <% if (user.photo_url) { %>
  71. // <div class="photo"><img src="<%= user.photo_url %>" /></div>
  72. // <% } %>
  73. // </div>
  74. //
  75. // Given that is a publicly accesible file, you would render it like:
  76. //
  77. // // app.js
  78. // $.sammy(function() {
  79. // // include the plugin
  80. // this.use('Template');
  81. //
  82. // this.get('#/', function() {
  83. // // the template is rendered in the current context.
  84. // this.user = {name: 'Aaron Quint'};
  85. // // partial calls template() because of the file extension
  86. // this.partial('user.template');
  87. // })
  88. // });
  89. //
  90. // You can also pass a second argument to use() that will alias the template
  91. // method and therefore allow you to use a different extension for template files
  92. // in <tt>partial()</tt>
  93. //
  94. // // alias to 'tpl'
  95. // this.use(Sammy.Template, 'tpl');
  96. //
  97. // // now .tpl files will be run through srender
  98. // this.get('#/', function() {
  99. // this.partial('myfile.tpl');
  100. // });
  101. //
  102. // By default, the data passed into the tempalate is passed automatically passed through
  103. // Sammy's `escapeHTML` method in order to prevent possible XSS attacks. This is
  104. // a problem though if you're using something like `Sammy.Form` which renders HTML
  105. // within the templates. You can get around this in two ways. One, you can use the
  106. // `<%! %>` instead of `<%= %>`. Two, you can pass the `escape_html = false` option
  107. // when interpolating, i.e:
  108. //
  109. // this.get('#/', function() {
  110. // this.template('myform.tpl', {form: "<form></form>"}, {escape_html: false});
  111. // });
  112. //
  113. Sammy.Template = function(app, method_alias) {
  114. // *Helper:* Uses simple templating to parse ERB like templates.
  115. //
  116. // ### Arguments
  117. //
  118. // * `template` A String template. '<% %>' tags are evaluated as Javascript and replaced with the elements in data.
  119. // * `data` An Object containing the replacement values for the template.
  120. // data is extended with the <tt>EventContext</tt> allowing you to call its methods within the template.
  121. // * `name` An optional String name to cache the template.
  122. //
  123. var template = function(template, data, name, options) {
  124. // use name for caching
  125. if (typeof name == 'undefined') { name = template; }
  126. if (typeof options == 'undefined' && typeof name == 'object') {
  127. options = name; name = template;
  128. }
  129. return srender(name, template, $.extend({}, this, data), options);
  130. };
  131. // set the default method name/extension
  132. if (!method_alias) { method_alias = 'template'; }
  133. // create the helper at the method alias
  134. app.helper(method_alias, template);
  135. };
  136. return Sammy.Template;
  137. }));