Guest User

Untitled

a guest
Jan 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Test</title>
  5. <style>
  6. *{font-family: sans-serif;}
  7. .table{
  8. border: 1px solid black;
  9. margin: 13px 2px;
  10. }
  11. .table th, td{
  12. padding: 3px 4px;
  13. }
  14. .table th{
  15. background: lightblue;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <script>
  21.  
  22. var data = {talking_to: "World", people: ["Derek", "Joe", "Alice", "Bob", "Veronika", "Zephyr"]};
  23.  
  24. var template = ['div',
  25. ['h1', {id: "title"},
  26. "Hello ", function(data){ return data.talking_to; }, "!" ],
  27. function(data){ return listPeople(data.people) },
  28. [VerticalTable, {class: 'table'},
  29. ['A', 'B', 'C'],
  30. ['1', '2', '3']]];
  31.  
  32. var inner_html = toHtml(template, data);
  33. console.log(inner_html);
  34. document.body.innerHTML = inner_html;
  35.  
  36. // a template returning a macro.
  37. function listPeople(persons){
  38. var result = [VerticalTable, {class: 'table'}, ["People"]];
  39. for(var i=0; i < persons.length; ++i){
  40. result.push([persons[i]]); }
  41. return result;
  42. }
  43.  
  44.  
  45. function VerticalTable(list, raise){
  46. var result = ["table"];
  47. var rows = [];
  48. var props = {};
  49.  
  50. list.slice(1).forEach(function(x, i){
  51. if(typeof x != "object"){
  52. raise("row_table each item must be list or props, got " + typeof x); }
  53. if(Array.isArray(x)) rows.push(x);
  54. else for(var k in x){ props[k] = x[k]; }});
  55.  
  56. result.push(props);
  57. console.info('vertical table rows', rows);
  58. for(var i=0; i < rows.length; ++i){
  59. var row = rows[i];
  60. var result_row = ["tr"];
  61. var tag = i==0? "th" : "td";
  62. result.push(result_row);
  63. for(var j=0; j < row.length; ++j){
  64. result_row.push([tag, row[j]]); }}
  65. return result;
  66. }
  67.  
  68.  
  69. function toHtml(list, data){
  70. return __toHtml(list, data, undefined); }
  71.  
  72. function __toHtml(list, data, root){
  73. if(root === undefined){
  74. root = list; }
  75. function raise(msg){
  76. console.error("toHtml " + msg)
  77. console.warn("list:", list)
  78. console.warn("data:", data);
  79. console.warn("root:", root);
  80. throw new Error("toHtml " + msg); }
  81. if(!data) data = {};
  82.  
  83. if(!Array.isArray(list)){
  84. raise("list required."); }
  85. var first = list[0];
  86. var tag = null;
  87. if(typeof first == "function"){
  88. var macro = first;
  89. var result = macro(list, raise);
  90. if(!Array.isArray(result)){
  91. raise("macro must return array."); }
  92. return __toHtml(result, data, root); }
  93. else if(typeof first == "string"){
  94. tag = first; }
  95. else{
  96. raise("unknown first type " + typeof first); }
  97.  
  98. var inner_html = "";
  99.  
  100. var props = {};
  101. for(var i=1; i<list.length; ++i){
  102. var item = list[i];
  103. if(typeof item == "function"){
  104. var template = item;
  105. var result = template(data, raise);
  106. if(Array.isArray(result)){
  107. console.info("Processing template macro", result);
  108. result = __toHtml(result, data, root); }
  109. if(typeof result != "string"){
  110. raise("template must return string, got " + typeof result + "."); }
  111. inner_html += result; }
  112. else if(typeof item == "string"){
  113. inner_html += item; }
  114. else if(Array.isArray(item)){
  115. inner_html += __toHtml(item, data, root); }
  116. else if(typeof item == "object"){
  117. for(var k in item){
  118. props[k] = item[k]; }}
  119. else{
  120. raise("unknown item type " + typeof item); }
  121. }
  122. if(tag === null){
  123. raise("tag unexpectedly null."); }
  124. var opening_tag = "<" + tag;
  125. for(var p in props){
  126. var value = props[p];
  127. if(typeof value == "function"){ // property template.
  128. var template = value;
  129. value = template(data, raise); }
  130. opening_tag += " " + p + "=\"" + value + "\""; }
  131. opening_tag += ">";
  132. var closing_tag = "</" + tag + ">";
  133. return opening_tag + inner_html + closing_tag;
  134. }
  135. </script>
  136. </body>
  137. </html>
Add Comment
Please, Sign In to add comment