templates.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var _ = require("lodash");
  2. function mker(helper, templatehelpers) {
  3. return function template(str, obj) {
  4. return (str + "").replace(/\{\{([a-zA-Z0-9-\|_\.\:/]*)\}\}/gi, function(a, m) {
  5. var re
  6. if (m.indexOf("::") > -1) {
  7. /* program */
  8. re = " TEST "
  9. var ma = m.split("::");
  10. fun = ma.shift();
  11. if (fun.indexOf("/") > -1) {
  12. //path mode ... go get
  13. var funs = fun.split("/");
  14. fun = funs.pop();
  15. var zfun = helper.oget(funs.join("/"))[fun]
  16. if (typeof(zfun) === "function") {
  17. re = zfun.apply(obj, ma);
  18. }
  19. } else {
  20. if (templatehelpers[fun]) {
  21. // rest skal være arguments
  22. re = templatehelpers[fun](obj);
  23. }
  24. if (obj[fun]) {
  25. if (typeof(obj[fun]) === "function") {
  26. re = obj[fun].apply(obj, ma)
  27. } else {
  28. // console.log("FUUUUUUUUUU", fun, ma, obj[fun])
  29. }
  30. }
  31. //
  32. }
  33. } else {
  34. if (m.indexOf("||") > -1) {
  35. var mm = m.split("||");
  36. m = mm.shift();
  37. re = (obj[m] ? obj[m] : gget(obj, m));
  38. if (re) {
  39. } else {
  40. m = mm.shift();
  41. re = (obj[m] ? obj[m] : gget(obj, m));
  42. }
  43. } else {
  44. re = (obj[m] ? obj[m] : gget(obj, m));
  45. if (typeof(re) === "undefined") {
  46. re = "";
  47. } else if (typeof(re) === "number") {
  48. re = re + "";
  49. } else if (typeof(re) === "date") {
  50. re = re + "";
  51. }
  52. if (typeof(re) === "object") {
  53. re = _.keys(re).map(function(k) {
  54. var ka = typeof(re[k]) === "function" ? re[k]() : re[k];
  55. return '<a href="#' + ka.path + '" class="ele">' + ka.path + '</a>'
  56. }).join(" ");
  57. } else {
  58. re = re + "";
  59. }
  60. if (re.indexOf("$REF") === 0) {
  61. return re.split("=").slice(1).join("=")
  62. } else {
  63. if (re.substring(0, 2) === "$$") {
  64. return helper.t_content(re.substring(2))
  65. }
  66. }
  67. }
  68. }
  69. return re
  70. })
  71. }
  72. }
  73. function gget(obj, path) {
  74. path = path || "";
  75. var arr = path.split(".");
  76. var t = obj;
  77. var done = false;
  78. if (arr.length) {
  79. while (arr.length && !done) {
  80. var check = arr.shift();
  81. if (check !== "") {
  82. t = t[check];
  83. }
  84. if (typeof t !== "object") {
  85. done = true;
  86. }
  87. }
  88. }
  89. return t;
  90. }
  91. function gset(obj, path, value) {
  92. if (typeof obj !== "object" || !obj) {
  93. throw new Error("obj is not Object");
  94. }
  95. if (typeof path !== "string" || path === "") {
  96. throw new Error("path must be string with length > 0");
  97. }
  98. var arr = path.split(".");
  99. var done = false;
  100. var t = obj;
  101. if (arr.length > 1) {
  102. while (arr.length && t && !done) {
  103. var check = arr.shift();
  104. if (typeof t[check] === "object" && arr.length > 0) {
  105. t = t[check];
  106. } else {
  107. done = true;
  108. arr.unshift(check);
  109. }
  110. }
  111. var xt = t;
  112. while (arr.length) {
  113. var tt = arr.shift();
  114. if (arr.length) { //go deeper
  115. xt = xt[tt] = {};
  116. } else {
  117. //last
  118. xt[tt] = value;
  119. }
  120. }
  121. } else {
  122. if (arr.length === 1 && arr[0] !== "") {
  123. t[arr[0]] = value;
  124. }
  125. }
  126. }
  127. module.exports = mker;