TemplateTag.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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', '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. //Build URL based on route id
  41. function url($id, $param = null, $addRootUrl = false) {
  42. Doo::loadHelper('DooUrlBuilder');
  43. // param pass in as string with format
  44. // 'param1=>this_is_my_value, param2=>something_here'
  45. if ($param != null) {
  46. $param = explode(', ', $param);
  47. $param2 = null;
  48. foreach ($param as $p) {
  49. $splited = explode('=>', $p);
  50. $param2[$splited[0]] = $splited[1];
  51. }
  52. return DooUrlBuilder::url($id, $param2, $addRootUrl);
  53. }
  54. return DooUrlBuilder::url($id, null, $addRootUrl);
  55. }
  56. //Build URL based on controller and method name
  57. function url2($controller, $method, $param = null, $addRootUrl = false) {
  58. Doo::loadHelper('DooUrlBuilder');
  59. // param pass in as string with format
  60. // 'param1=>this_is_my_value, param2=>something_here'
  61. if ($param != null) {
  62. $param = explode(', ', $param);
  63. $param2 = null;
  64. foreach ($param as $p) {
  65. $splited = explode('=>', $p);
  66. $param2[$splited[0]] = $splited[1];
  67. }
  68. return DooUrlBuilder::url2($controller, $method, $param2, $addRootUrl);
  69. }
  70. return DooUrlBuilder::url2($controller, $method, null, $addRootUrl);
  71. }
  72. function ToChinaseNum($num) {
  73. $char = array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
  74. $dw = array("", "十", "百", "千", "万", "亿", "兆");
  75. $retval = "";
  76. $proZero = false;
  77. for ($i = 0; $i < strlen($num); $i++) {
  78. if ($i > 0)
  79. $temp = (int) (($num % pow(10, $i + 1)) / pow(10, $i));
  80. else
  81. $temp = (int) ($num % pow(10, 1));
  82. if ($proZero == true && $temp == 0)
  83. continue;
  84. if ($temp == 0)
  85. $proZero = true;
  86. else
  87. $proZero = false;
  88. if ($proZero) {
  89. if ($retval == "")
  90. continue;
  91. $retval = $char[$temp] . $retval;
  92. } else
  93. $retval = $char[$temp] . $dw[$i] . $retval;
  94. }
  95. if ($retval == "一十")
  96. $retval = "十";
  97. return $retval;
  98. }
  99. ?>