TemplateTag.php 3.3 KB

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