TemplateTag.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. //register global/PHP functions to be used with your template files
  3. //You can move this to common.conf.php $config['TEMPLATE_GLOBAL_TAGS'] = array('isset', 'empty');
  4. //Every public static methods in TemplateTag class (or tag classes from modules) are available in templates without the need to define in TEMPLATE_GLOBAL_TAGS
  5. Doo::conf()->TEMPLATE_GLOBAL_TAGS = array('upper', 'tofloat','trrep', 'unsetck' ,'sample_with_args', 'debug', 'url', 'url2', 'function_deny', 'isset', 'empty', 'formatDate', 'zh_in_array', 'zh_empty', 'fileExists', 'odd', 'mbsub', 'zhisset', 'totime');
  6. /**
  7. Define as class (optional)
  8. class TemplateTag {
  9. public static test(){}
  10. public static test2(){}
  11. }
  12. * */
  13. function zhisset($str) {
  14. return isset($str);
  15. }
  16. function upper($str) {
  17. return strtoupper($str);
  18. }
  19. function tofloat($str) {
  20. return sprintf("%.2f", $str);
  21. }
  22. function unsetck($str){
  23. setcookie ( $str, "", time () + 3600 * 24, "/", COOKIE_WEB_SITE );
  24. return true;
  25. }
  26. function sample_with_args($str, $prefix) {
  27. return $str . ' with args: ' . $prefix;
  28. }
  29. function debug($var) {
  30. if (!empty($var)) {
  31. echo '<pre>';
  32. print_r($var);
  33. echo '</pre>';
  34. }
  35. }
  36. //This will be called when a function NOT Registered is used in IF or ElseIF statment
  37. function function_deny($var = null) {
  38. echo '<span style="color:#ff0000;">Function denied in IF or ElseIF statement!</span>';
  39. exit;
  40. }
  41. //Build URL based on route id
  42. function url($id, $param = null, $addRootUrl = false) {
  43. Doo::loadHelper('DooUrlBuilder');
  44. // param pass in as string with format
  45. // 'param1=>this_is_my_value, param2=>something_here'
  46. if ($param != null) {
  47. $param = explode(', ', $param);
  48. $param2 = null;
  49. foreach ($param as $p) {
  50. $splited = explode('=>', $p);
  51. $param2[$splited[0]] = $splited[1];
  52. }
  53. return DooUrlBuilder::url($id, $param2, $addRootUrl);
  54. }
  55. return DooUrlBuilder::url($id, null, $addRootUrl);
  56. }
  57. //Build URL based on controller and method name
  58. function url2($controller, $method, $param = null, $addRootUrl = false) {
  59. Doo::loadHelper('DooUrlBuilder');
  60. // param pass in as string with format
  61. // 'param1=>this_is_my_value, param2=>something_here'
  62. if ($param != null) {
  63. $param = explode(', ', $param);
  64. $param2 = null;
  65. foreach ($param as $p) {
  66. $splited = explode('=>', $p);
  67. $param2[$splited[0]] = $splited[1];
  68. }
  69. return DooUrlBuilder::url2($controller, $method, $param2, $addRootUrl);
  70. }
  71. return DooUrlBuilder::url2($controller, $method, null, $addRootUrl);
  72. }
  73. function formatDate($date, $format = 'Y-m-d H:i:s') {
  74. return date($format, $date);
  75. }
  76. function zh_in_array($value, $arr) {
  77. return in_array($value, $arr);
  78. }
  79. function trrep($str){
  80. return str_replace("à","m3",str_replace("'","",$str));
  81. }
  82. function zh_empty($value) {
  83. return empty($value);
  84. }
  85. function fileExists($filename) {
  86. return file_exists($filename);
  87. }
  88. function odd($rownum) {
  89. return $rownum % 2;
  90. }
  91. function mbsub($content) {
  92. return mb_substr($content, 0, 35, 'utf-8');
  93. }
  94. function totime($s){
  95. if($s<10){
  96. return '00:00:0'.$s;
  97. }elseif($s<60){
  98. return '00:00:'.$s;
  99. }elseif($s<3600){
  100. $m = floor($s/60);
  101. $s = $s%60;
  102. if($m<10){
  103. $m = '0'.$m;
  104. }
  105. if($s<10){
  106. $s = '0'.$s;
  107. }
  108. return '00:'.$m.':'.$s;
  109. }elseif($s>3600){
  110. $h = floor($s/3600);
  111. $m = floor($s/60);
  112. $s = $s%60;
  113. if($h<10){
  114. $h = '0'.$h;
  115. }
  116. if($m<10){
  117. $m = '0'.$m;
  118. }
  119. if($s<10){
  120. $s = '0'.$s;
  121. }
  122. return $h.':'.$m.':'.$s;
  123. }
  124. }
  125. ?>