Template.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace Heanup\Frame;
  3. use Heanup\Config\Lang\Lang;
  4. /**
  5. * 简易模板类
  6. */
  7. class Template
  8. {
  9. private $data = array();
  10. private $path;
  11. private $tpl = null;
  12. private $suffix = '.phtml';
  13. public function __isset($name)
  14. {
  15. return isset($this->data[$name]);
  16. }
  17. public function __set($name, $value = null)
  18. {
  19. $this->data[$name] = $value;
  20. }
  21. public function __get($name)
  22. {
  23. return isset($this->data[$name]) ? $this->data[$name] : null;
  24. }
  25. /**
  26. * 设置模板路径
  27. * @param $path
  28. * @return $this
  29. */
  30. public function setPath($path)
  31. {
  32. if ($path) {
  33. $this->path = rtrim($path, '/') . DIRECTORY_SEPARATOR;
  34. }
  35. return $this;
  36. }
  37. /**
  38. * 设置模板后缀
  39. * @param $suffix
  40. * @return $this
  41. */
  42. public function setSuffix($suffix)
  43. {
  44. if ($suffix) {
  45. $this->suffix = $suffix;
  46. }
  47. return $this;
  48. }
  49. /**
  50. * 设置模板名称
  51. * @param $tpl
  52. * @return $this
  53. */
  54. public function setTpl($tpl)
  55. {
  56. if ($tpl) {
  57. $this->tpl = $tpl;
  58. }
  59. return $this;
  60. }
  61. /**
  62. * 变量赋值, 可以单独赋值,也可以批量赋值
  63. * @param $name
  64. * @param null $value
  65. * @return $this
  66. */
  67. public function assign($name, $value = null)
  68. {
  69. if (is_array($name)) {
  70. foreach ($name as $k => $v) {
  71. $this->data[$k] = $v;
  72. }
  73. } else {
  74. $this->data[$name] = $value;
  75. }
  76. return $this;
  77. }
  78. /**
  79. * 获取全部数据
  80. * @return array
  81. */
  82. public function getData()
  83. {
  84. return $this->data;
  85. }
  86. /**
  87. * 载入其他模板
  88. * @param $tpl
  89. * @return null
  90. */
  91. public function load($tpl)
  92. {
  93. $tpl = $this->path . $tpl . $this->suffix;
  94. if (is_readable($tpl)) {
  95. include($tpl);
  96. } else {
  97. return null;
  98. }
  99. }
  100. /**
  101. * 渲染页面
  102. * @param bool $isReturn
  103. * @param string $layout
  104. * @return string
  105. */
  106. public function render($isReturn = false, $layout = '')
  107. {
  108. $filename = $this->path . $this->tpl . $this->suffix;
  109. // 判断模板是否存在
  110. if (!is_readable($filename)) {
  111. Logger::error($filename, 'template.not_found');
  112. return '';
  113. }
  114. // 判断layout是否存在
  115. if ($layout && is_readable($this->path . $layout . $this->suffix)) {
  116. $filename = $this->path . $layout . $this->suffix;
  117. }
  118. if ($isReturn == false) {
  119. include($filename);
  120. } else {
  121. ob_start();
  122. include($filename);
  123. $content = ob_get_contents();
  124. ob_end_clean();
  125. return $content;
  126. }
  127. }
  128. /**
  129. * 输出ajax信息
  130. * @param bool $result
  131. * @param null $code
  132. * @param null $msg
  133. * @param null $data
  134. */
  135. public function output($result = true, $code = null, $msg = null, $data = null)
  136. {
  137. if ($result instanceof Exception) {
  138. $code = $result->getCode();
  139. $msg = $result->getMessage();
  140. $data = $result->__toString();
  141. $result = false;
  142. }
  143. if ($result AND !is_bool($result) AND is_null($data)) {
  144. $data = $result;
  145. }
  146. $result = (bool)$result;
  147. $code = (int)$code;
  148. $msg = (!empty($msg)) ? $msg : '';
  149. $data = (!empty($data)) ? $data : '';
  150. $data = array(
  151. 'result' => $result,
  152. 'code' => $code,
  153. 'msg' => $msg,
  154. 'data' => $data,
  155. );
  156. header('Content-type: application/json');
  157. echo json_encode($data);
  158. }
  159. /**
  160. * 获取同域名下的其他子域名
  161. * @param $domain
  162. * @return string
  163. */
  164. protected function getDomainHost($domain)
  165. {
  166. $domainHost = $_SERVER['HTTP_HOST'];
  167. $domainHost = $domainHost."/".$domain;
  168. return $domainHost;
  169. }
  170. /**
  171. * 获取多语言配置文件
  172. * @param string $key key
  173. * @param string $default 默认值
  174. */
  175. protected function getLang($key,$default="",$app="Admin",$lang=NULL){
  176. return Lang::getData($key,$default,$app,$lang);
  177. }
  178. protected function getPager($pager){
  179. // if($pager['total_page'] == 0 || $pager['total_page'] <= $pager['page_size']){
  180. // return '';
  181. // }
  182. $current_start = ($pager['current_page']-1)*$pager['page_size']+1;
  183. $current_end = $pager['current_page']*$pager['page_size'] < $pager['total_count'] ? $pager['current_page']*$pager['page_size']:$pager['total_count'];
  184. $html = '<div class="row">
  185. <div class="col-sm-5">
  186. <div class="dataTables_info">当前显示 '.$current_start.' 到 '.$current_end.' 条,共 '.$pager['total_count'].' 条记录</div>
  187. </div><div class="col-sm-7">
  188. <div class="text-right">
  189. <ul class="pagination" style="margin:0">';
  190. $start = 1;
  191. $skip = 0;
  192. $end = $pager['total_page'];
  193. if($pager['current_page'] - 3 > $start){
  194. $start = $pager['current_page'] - 3;
  195. $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page=1&'.$pager['filter'].'">首页</a></li>';
  196. }else{
  197. $skip = 4 - $pager['current_page'];
  198. }
  199. if($pager['current_page'] + 3 < $end){
  200. $end = $pager['current_page'] + 3 + $skip;
  201. }
  202. for($i = $start; $i <= $end; $i++){
  203. if($i == $pager['current_page']){
  204. $html .= '<li class="paginate_button active"><a href="javascript:;">'.$i.'</a></li>';
  205. }else{
  206. $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page='.$i.'&'.$pager['filter'].'">'.$i.'</a></li>';
  207. }
  208. }
  209. if($end < $pager['total_page']){
  210. $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page='.$pager['total_page'].'&'.$pager['filter'].'">末页</a></li>';
  211. }
  212. $html .= '</ul></div>';
  213. return $html;
  214. }
  215. }