Template.php 5.8 KB

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