TemplateTag.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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', 'zlong2ip', 'debug', 'url', 'url2', 'function_deny', 'isset', 'empty', 'formatDate', 'formatDateTime', 'zh_in_array', 'zh_empty', 'fileExists', 'odd');
  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. //Build URL based on route id
  35. function url($id, $param = null, $addRootUrl = false) {
  36. Doo::loadHelper('DooUrlBuilder');
  37. // param pass in as string with format
  38. // 'param1=>this_is_my_value, param2=>something_here'
  39. if ($param != null) {
  40. $param = explode(', ', $param);
  41. $param2 = null;
  42. foreach ($param as $p) {
  43. $splited = explode('=>', $p);
  44. $param2[$splited[0]] = $splited[1];
  45. }
  46. return DooUrlBuilder::url($id, $param2, $addRootUrl);
  47. }
  48. return DooUrlBuilder::url($id, null, $addRootUrl);
  49. }
  50. //Build URL based on controller and method name
  51. function url2($controller, $method, $param = null, $addRootUrl = false) {
  52. Doo::loadHelper('DooUrlBuilder');
  53. // param pass in as string with format
  54. // 'param1=>this_is_my_value, param2=>something_here'
  55. if ($param != null) {
  56. $param = explode(', ', $param);
  57. $param2 = null;
  58. foreach ($param as $p) {
  59. $splited = explode('=>', $p);
  60. $param2[$splited[0]] = $splited[1];
  61. }
  62. return DooUrlBuilder::url2($controller, $method, $param2, $addRootUrl);
  63. }
  64. return DooUrlBuilder::url2($controller, $method, null, $addRootUrl);
  65. }
  66. function formatDate($date, $format = 'Y-m-d H:i:s') {
  67. return date($format, $date);
  68. }
  69. function formatDateTime($date, $format = 'Y-m-d') {
  70. return date($format, $date);
  71. }
  72. function zh_in_array($value, $arr) {
  73. return in_array($value, $arr);
  74. }
  75. function zh_empty($value) {
  76. return empty($value);
  77. }
  78. function fileExists($filename) {
  79. return file_exists($filename);
  80. }
  81. function odd($rownum) {
  82. return $rownum % 2;
  83. }
  84. function zlong2ip($ip) {
  85. return long2ip($ip);
  86. }
  87. ?>