| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace Heanup\Frame;
- /**
- * 简易模板类
- */
- class Template
- {
- private $data = array();
- private $path;
- private $tpl = null;
- private $suffix = '.phtml';
- public function __isset($name)
- {
- return isset($this->data[$name]);
- }
- public function __set($name, $value = null)
- {
- $this->data[$name] = $value;
- }
- public function __get($name)
- {
- return isset($this->data[$name]) ? $this->data[$name] : null;
- }
- /**
- * 设置模板路径
- * @param $path
- * @return $this
- */
- public function setPath($path)
- {
- if ($path) {
- $this->path = rtrim($path, '/') . DIRECTORY_SEPARATOR;
- }
- return $this;
- }
- /**
- * 设置模板后缀
- * @param $suffix
- * @return $this
- */
- public function setSuffix($suffix)
- {
- if ($suffix) {
- $this->suffix = $suffix;
- }
- return $this;
- }
- /**
- * 设置模板名称
- * @param $tpl
- * @return $this
- */
- public function setTpl($tpl)
- {
- if ($tpl) {
- $this->tpl = $tpl;
- }
- return $this;
- }
- /**
- * 变量赋值, 可以单独赋值,也可以批量赋值
- * @param $name
- * @param null $value
- * @return $this
- */
- public function assign($name, $value = null)
- {
- if (is_array($name)) {
- foreach ($name as $k => $v) {
- $this->data[$k] = $v;
- }
- } else {
- $this->data[$name] = $value;
- }
- return $this;
- }
- /**
- * 获取全部数据
- * @return array
- */
- public function getData()
- {
- return $this->data;
- }
- /**
- * 载入其他模板
- * @param $tpl
- * @return null
- */
- public function load($tpl)
- {
- $tpl = $this->path . $tpl . $this->suffix;
- if (is_readable($tpl)) {
- include($tpl);
- } else {
- return null;
- }
- }
- /**
- * 渲染页面
- * @param bool $isReturn
- * @param string $layout
- * @return string
- */
- public function render($isReturn = false, $layout = '')
- {
- $filename = $this->path . $this->tpl . $this->suffix;
- // 判断模板是否存在
- if (!is_readable($filename)) {
- Logger::error($filename, 'template.not_found');
- return '';
- }
- // 判断layout是否存在
- if ($layout && is_readable($this->path . $layout . $this->suffix)) {
- $filename = $this->path . $layout . $this->suffix;
- }
- if ($isReturn == false) {
- include($filename);
- } else {
- ob_start();
- include($filename);
- $content = ob_get_contents();
- ob_end_clean();
- return $content;
- }
- }
- /**
- * 输出ajax信息
- * @param bool $result
- * @param null $code
- * @param null $msg
- * @param null $data
- */
- public function output($result = true, $code = null, $msg = null, $data = null)
- {
- if ($result instanceof Exception) {
- $code = $result->getCode();
- $msg = $result->getMessage();
- $data = $result->__toString();
- $result = false;
- }
- if ($result AND !is_bool($result) AND is_null($data)) {
- $data = $result;
- }
- $result = (bool)$result;
- $code = (int)$code;
- $msg = (!empty($msg)) ? $msg : '';
- $data = (!empty($data)) ? $data : '';
- $data = array(
- 'result' => $result,
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data,
- );
- header('Content-type: application/json');
- echo json_encode($data);
- }
- /**
- * 获取同域名下的其他子域名
- * @param $domain
- * @return string
- */
- protected function getDomainHost($domain)
- {
- $domainHost = $_SERVER['HTTP_HOST'];
- $domainHost = $domainHost."/".$domain;
- return $domainHost;
- }
- protected function getPager($pager){
- // if($pager['total_page'] == 0 || $pager['total_page'] <= $pager['page_size']){
- // return '';
- // }
- $current_start = ($pager['current_page']-1)*$pager['page_size']+1;
- $current_end = $pager['current_page']*$pager['page_size'] < $pager['total_count'] ? $pager['current_page']*$pager['page_size']:$pager['total_count'];
- $html = '<div class="row">
- <div class="col-sm-5">
- <div class="dataTables_info">当前显示 '.$current_start.' 到 '.$current_end.' 条,共 '.$pager['total_count'].' 条记录</div>
- </div><div class="col-sm-7">
- <div class="text-right">
- <ul class="pagination" style="margin:0">';
- $start = 1;
- $skip = 0;
- $end = $pager['total_page'];
- if($pager['current_page'] - 3 > $start){
- $start = $pager['current_page'] - 3;
- $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page=1&'.$pager['filter'].'">首页</a></li>';
- }else{
- $skip = 4 - $pager['current_page'];
- }
- if($pager['current_page'] + 3 < $end){
- $end = $pager['current_page'] + 3 + $skip;
- }
- for($i = $start; $i <= $end; $i++){
- if($i == $pager['current_page']){
- $html .= '<li class="paginate_button active"><a href="javascript:;">'.$i.'</a></li>';
- }else{
- $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page='.$i.'&'.$pager['filter'].'">'.$i.'</a></li>';
- }
- }
- if($end < $pager['total_page']){
- $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page='.$pager['total_page'].'&'.$pager['filter'].'">末页</a></li>';
- }
- $html .= '</ul></div>';
- return $html;
- }
- }
|