TemplateTag.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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', 'sample_with_args', 'debug', 'url', 'url2', 'function_deny', 'zhisset', 'in_phparray', 'empty', 'formatDate', 'ToChinaseNum');
  6. /**
  7. Define as class (optional)
  8. class TemplateTag {
  9. public static test(){}
  10. public static test2(){}
  11. }
  12. * */
  13. function upper($str) {
  14. return strtoupper($str);
  15. }
  16. function tofloat($str) {
  17. return sprintf("%.2f", $str);
  18. }
  19. function sample_with_args($str, $prefix) {
  20. return $str . ' with args: ' . $prefix;
  21. }
  22. function debug($var) {
  23. if (!empty($var)) {
  24. echo '<pre>';
  25. print_r($var);
  26. echo '</pre>';
  27. }
  28. }
  29. //This will be called when a function NOT Registered is used in IF or ElseIF statment
  30. function function_deny($var = null) {
  31. echo '<span style="color:#ff0000;">Function denied in IF or ElseIF statement!</span>';
  32. exit;
  33. }
  34. function formatDate($date, $format = 'Y-m-d H:i:s') {
  35. return date($format, $date);
  36. }
  37. function zhisset($val) {
  38. return isset($val);
  39. }
  40. function in_phparray($val,$list){
  41. $list = explode(',',$list);
  42. return in_array($val,$list);
  43. }
  44. //Build URL based on route id
  45. function url($id, $param = null, $addRootUrl = false) {
  46. Doo::loadHelper('DooUrlBuilder');
  47. // param pass in as string with format
  48. // 'param1=>this_is_my_value, param2=>something_here'
  49. if ($param != null) {
  50. $param = explode(', ', $param);
  51. $param2 = null;
  52. foreach ($param as $p) {
  53. $splited = explode('=>', $p);
  54. $param2[$splited[0]] = $splited[1];
  55. }
  56. return DooUrlBuilder::url($id, $param2, $addRootUrl);
  57. }
  58. return DooUrlBuilder::url($id, null, $addRootUrl);
  59. }
  60. //Build URL based on controller and method name
  61. function url2($controller, $method, $param = null, $addRootUrl = false) {
  62. Doo::loadHelper('DooUrlBuilder');
  63. // param pass in as string with format
  64. // 'param1=>this_is_my_value, param2=>something_here'
  65. if ($param != null) {
  66. $param = explode(', ', $param);
  67. $param2 = null;
  68. foreach ($param as $p) {
  69. $splited = explode('=>', $p);
  70. $param2[$splited[0]] = $splited[1];
  71. }
  72. return DooUrlBuilder::url2($controller, $method, $param2, $addRootUrl);
  73. }
  74. return DooUrlBuilder::url2($controller, $method, null, $addRootUrl);
  75. }
  76. function ToChinaseNum($num) {
  77. $char = array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
  78. $dw = array("", "十", "百", "千", "万", "亿", "兆");
  79. $retval = "";
  80. $proZero = false;
  81. for ($i = 0; $i < strlen($num); $i++) {
  82. if ($i > 0)
  83. $temp = (int) (($num % pow(10, $i + 1)) / pow(10, $i));
  84. else
  85. $temp = (int) ($num % pow(10, 1));
  86. if ($proZero == true && $temp == 0)
  87. continue;
  88. if ($temp == 0)
  89. $proZero = true;
  90. else
  91. $proZero = false;
  92. if ($proZero) {
  93. if ($retval == "")
  94. continue;
  95. $retval = $char[$temp] . $retval;
  96. } else
  97. $retval = $char[$temp] . $dw[$i] . $retval;
  98. }
  99. if ($retval == "一十")
  100. $retval = "十";
  101. return $retval;
  102. }
  103. ?>