4cd2ebfbab75f03b1fb4d60716750736da614771.svn-base 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. if (!dojo._hasResource["dojox.dtl.tag.loader"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.dtl.tag.loader"] = true;
  6. dojo.provide("dojox.dtl.tag.loader");
  7. dojo.require("dojox.dtl._base");
  8. dojox.dtl.tag.loader.BlockNode = function(name, nodelist) {
  9. this.name = name;
  10. this.nodelist = nodelist; // Can be overridden
  11. }
  12. dojo.extend(dojox.dtl.tag.loader.BlockNode, {
  13. render : function(context, buffer) {
  14. if (this.override) {
  15. buffer = this.override.render(context, buffer, this);
  16. this.rendered = this.override;
  17. } else {
  18. buffer = this.nodelist.render(context, buffer, this);
  19. this.rendered = this.nodelist;
  20. }
  21. this.override = null;
  22. return buffer;
  23. },
  24. unrender : function(context, buffer) {
  25. return this.rendered.unrender(context, buffer);
  26. },
  27. setOverride : function(nodelist) {
  28. // summary: In a shared parent, we override, not overwrite
  29. if (!this.override) {
  30. this.override = nodelist;
  31. }
  32. },
  33. toString : function() {
  34. return "dojox.dtl.tag.loader.BlockNode";
  35. }
  36. });
  37. dojox.dtl.tag.loader.block = function(parser, text) {
  38. var parts = text.split(" ");
  39. var name = parts[1];
  40. parser._blocks = parser._blocks || {};
  41. parser._blocks[name] = parser._blocks[name] || [];
  42. parser._blocks[name].push(name);
  43. var nodelist = parser.parse(["endblock", "endblock " + name]);
  44. parser.next();
  45. return new dojox.dtl.tag.loader.BlockNode(name, nodelist);
  46. }
  47. dojox.dtl.tag.loader.ExtendsNode = function(getTemplate, nodelist, shared,
  48. parent, key) {
  49. this.getTemplate = getTemplate;
  50. this.nodelist = nodelist;
  51. this.shared = shared;
  52. this.parent = parent;
  53. this.key = key;
  54. }
  55. dojo.extend(dojox.dtl.tag.loader.ExtendsNode, {
  56. parents : {},
  57. getParent : function(context) {
  58. if (!this.parent) {
  59. this.parent = context.get(this.key, false);
  60. if (!this.parent) {
  61. throw new Error("extends tag used a variable that did not resolve");
  62. }
  63. if (typeof this.parent == "object") {
  64. if (this.parent.url) {
  65. if (this.parent.shared) {
  66. this.shared = true;
  67. }
  68. this.parent = this.parent.url.toString();
  69. } else {
  70. this.parent = this.parent.toString();
  71. }
  72. }
  73. if (this.parent && this.parent.indexOf("shared:") == 0) {
  74. this.shared = true;
  75. this.parent = this.parent.substring(7, parent.length);
  76. }
  77. }
  78. var parent = this.parent;
  79. if (!parent) {
  80. throw new Error("Invalid template name in 'extends' tag.");
  81. }
  82. if (parent.render) {
  83. return parent;
  84. }
  85. if (this.parents[parent]) {
  86. return this.parents[parent];
  87. }
  88. this.parent = this.getTemplate(dojox.dtl.text
  89. .getTemplateString(parent));
  90. if (this.shared) {
  91. this.parents[parent] = this.parent;
  92. }
  93. return this.parent;
  94. },
  95. render : function(context, buffer) {
  96. var st = dojox.dtl;
  97. var stbl = dojox.dtl.tag.loader;
  98. var parent = this.getParent(context);
  99. var isChild = parent.nodelist[0] instanceof this.constructor;
  100. var parentBlocks = {};
  101. for (var i = 0, node; node = parent.nodelist.contents[i]; i++) {
  102. if (node instanceof stbl.BlockNode) {
  103. parentBlocks[node.name] = node;
  104. }
  105. }
  106. for (var i = 0, node; node = this.nodelist.contents[i]; i++) {
  107. if (node instanceof stbl.BlockNode) {
  108. var block = parentBlocks[node.name];
  109. if (!block) {
  110. if (isChild) {
  111. parent.nodelist[0].nodelist.append(node);
  112. }
  113. } else {
  114. if (this.shared) {
  115. block.setOverride(node.nodelist);
  116. } else {
  117. block.nodelist = node.nodelist;
  118. }
  119. }
  120. }
  121. }
  122. this.rendered = parent;
  123. return parent.render(context, buffer, this);
  124. },
  125. unrender : function(context, buffer) {
  126. return this.rendered.unrender(context, buffer, this);
  127. },
  128. toString : function() {
  129. return "dojox.dtl.block.ExtendsNode";
  130. }
  131. });
  132. dojox.dtl.tag.loader.extends_ = function(parser, text) {
  133. var parts = text.split(" ");
  134. var shared = false;
  135. var parent = null;
  136. var key = null;
  137. if (parts[1].charAt(0) == '"' || parts[1].charAt(0) == "'") {
  138. parent = parts[1].substring(1, parts[1].length - 1);
  139. } else {
  140. key = parts[1];
  141. }
  142. if (parent && parent.indexOf("shared:") == 0) {
  143. shared = true;
  144. parent = parent.substring(7, parent.length);
  145. }
  146. var nodelist = parser.parse();
  147. return new dojox.dtl.tag.loader.ExtendsNode(parser.getTemplate,
  148. nodelist, shared, parent, key);
  149. }
  150. }