TemplateTag.php 3.3 KB

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