smarty.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs;
  3. use MGModule\DNSManager2 as main;
  4. use MGModule\DNSManager2\mgLibs\custom\helpers\HTML5Pattern;
  5. /**
  6. * Smarty Wrapper
  7. *
  8. * @author Michal Czech <michael@modulesgarden.com>
  9. */
  10. class smarty {
  11. static private $_instance;
  12. private $_smarty;
  13. private $_templateDIR;
  14. final private function __construct(){}
  15. final private function __clone(){}
  16. /**
  17. * Get Single-top Instance
  18. *
  19. * @author Michal Czech <michael@modulesgarden.com>
  20. * @return smarty
  21. */
  22. static function I(){
  23. if(empty(self::$_instance))
  24. {
  25. self::$_instance = new self();
  26. if(!class_exists('Smarty'))
  27. {
  28. if(file_exists(ROOTDIR.DS.'includes'.DS.'smarty'.DS.'Smarty.class.php'))
  29. {
  30. require_once(ROOTDIR.DS.'includes'.DS.'smarty'.DS.'Smarty.class.php');
  31. }
  32. else
  33. {
  34. die('Smarty does not exists!');
  35. }
  36. }
  37. self::$_instance->_smarty = new \Smarty();
  38. }
  39. return self::$_instance;
  40. }
  41. /**
  42. * Set Tempalte Dir
  43. *
  44. * @author Michal Czech <michael@modulesgarden.com>
  45. * @param string $dir
  46. */
  47. function setTemplateDir($dir){
  48. if(is_array($dir))
  49. {
  50. throw new exceptions\system('Wrong Template Path');
  51. }
  52. self::I()->_templateDIR = $dir;
  53. }
  54. /**
  55. * Parse Template File
  56. *
  57. * @author Michal Czech <michael@modulesgarden.com>
  58. * @global string $templates_compiledir
  59. * @param string $template
  60. * @param array $vars
  61. * @param string $customDir
  62. * @return string
  63. * @throws exceptions\system
  64. */
  65. function view($template,$vars = array(), $customDir = false){
  66. if(is_array($customDir))
  67. {
  68. throw new exceptions\system('Wrong Template Path');
  69. }
  70. global $templates_compiledir;
  71. if($customDir)
  72. {
  73. self::I()->_smarty->template_dir = $customDir;
  74. }
  75. else
  76. {
  77. self::I()->_smarty->template_dir = self::I()->_templateDIR;
  78. }
  79. self::I()->_smarty->compile_dir = $templates_compiledir;
  80. self::I()->_smarty->force_compile = 1;
  81. self::I()->_smarty->caching = 0;
  82. self::I()->_smarty->clearAllAssign();
  83. self::I()->_smarty->assign('MGLANG', lang::getInstance());
  84. self::I()->_smarty->assign('patterns', HTML5Pattern::getPatternsArray());
  85. if(is_array($vars))
  86. {
  87. foreach($vars as $key => $val)
  88. {
  89. self::I()->_smarty->assign($key, $val);
  90. }
  91. }
  92. if(is_array(self::I()->_smarty->template_dir))
  93. {
  94. $file = self::I()->_smarty->template_dir[0].DS.$template.'.tpl';
  95. }
  96. else
  97. {
  98. $file = self::I()->_smarty->template_dir.DS.$template.'.tpl';
  99. }
  100. if(!file_exists($file))
  101. {
  102. throw new exceptions\system('Unable to find Template:'.$file);
  103. }
  104. return self::I()->_smarty->fetch($template.'.tpl', time());
  105. }
  106. }