tpl.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * 模板驱动
  4. *
  5. * 模板驱动,商城模板引擎
  6. *
  7. *
  8. * @package tpl
  9. */
  10. defined('InShopNC') or exit('Access Invalid!');
  11. class Tpl
  12. {
  13. /**
  14. * 单件对象
  15. */
  16. private static $instance = null;
  17. /**
  18. * 输出模板内容的数组,其他的变量不允许从程序中直接输出到模板
  19. */
  20. private static $output_value = array();
  21. /**
  22. * 模板路径设置
  23. */
  24. private static $tpl_dir = '';
  25. /**
  26. * 默认layout
  27. */
  28. private static $layout_file = 'layout';
  29. private function __construct()
  30. {
  31. }
  32. /**
  33. * 实例化
  34. *
  35. * @return obj
  36. */
  37. public static function getInstance()
  38. {
  39. if (self::$instance === null || !(self::$instance instanceof Tpl)) {
  40. self::$instance = new Tpl();
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * 设置模板目录
  46. *
  47. * @param string $dir
  48. * @return bool
  49. */
  50. public static function setDir($dir)
  51. {
  52. self::$tpl_dir = $dir;
  53. return true;
  54. }
  55. /**
  56. * 设置布局
  57. *
  58. * @param string $layout
  59. * @return bool
  60. */
  61. public static function setLayout($layout)
  62. {
  63. self::$layout_file = $layout;
  64. return true;
  65. }
  66. /**
  67. * 抛出变量
  68. *
  69. * @param mixed $output
  70. * @param void
  71. */
  72. public static function output($output, $input = '')
  73. {
  74. self::getInstance();
  75. self::$output_value[$output] = $input;
  76. }
  77. public static function clear()
  78. {
  79. self::getInstance();
  80. self::$output_value = array();
  81. }
  82. /**
  83. * 调用显示模板
  84. *
  85. * @param string $page_name
  86. * @param string $layout
  87. * @param int $time
  88. */
  89. public static function showpage($page_name = '', $layout = '', $time = 2000)
  90. {
  91. if (!defined('TPL_NAME')) define('TPL_NAME', 'default');
  92. self::getInstance();
  93. if (!empty(self::$tpl_dir)) {
  94. $tpl_dir = self::$tpl_dir . DS;
  95. }
  96. //默认是带有布局文件
  97. if (empty($layout)) {
  98. $layout = 'layout' . DS . self::$layout_file . '.php';
  99. } else {
  100. $layout = 'layout' . DS . $layout . '.php';
  101. }
  102. $layout_file = BASE_PATH . '/templates/' . TPL_NAME . DS . $layout;
  103. $tpl_file = BASE_PATH . '/templates/' . TPL_NAME . DS . $tpl_dir . $page_name . '.php';
  104. if (file_exists($tpl_file))
  105. {
  106. //对模板变量进行赋值
  107. $output = self::$output_value;
  108. //页头
  109. $output['html_title'] = $output['html_title'] != '' ? $output['html_title'] : $GLOBALS['setting_config']['site_name'];
  110. $output['seo_keywords'] = $output['seo_keywords'] != '' ? $output['seo_keywords'] : $GLOBALS['setting_config']['site_name'];
  111. $output['seo_description'] = $output['seo_description'] != '' ? $output['seo_description'] : $GLOBALS['setting_config']['site_name'];
  112. $output['ref_url'] = getReferer();
  113. Language::read('common');
  114. $lang = Language::getLangContent();
  115. @header("Content-type: text/html; charset=" . CHARSET);
  116. //判断是否使用布局方式输出模板,如果是,那么包含布局文件,并且在布局文件中包含模板文件
  117. if ($layout != '')
  118. {
  119. if (file_exists($layout_file)) {
  120. include($layout_file);
  121. } else {
  122. $error = 'Tpl ERROR:' . 'templates' . DS . $layout . ' is not exists';
  123. throw_exception($error);
  124. }
  125. } else {
  126. include_once($tpl_file);
  127. }
  128. } else {
  129. $error = 'Tpl ERROR:' . 'templates' . DS . $tpl_dir . $page_name . '.php' . ' is not exists';
  130. throw_exception($error);
  131. }
  132. }
  133. /**
  134. * 显示页面Trace信息
  135. *
  136. * @return array
  137. */
  138. public static function showTrace()
  139. {
  140. $trace = array();
  141. //当前页面
  142. $trace[Language::get('nc_debug_current_page')] = $_SERVER['REQUEST_URI'] . '<br>';
  143. //请求时间
  144. $trace[Language::get('nc_debug_request_time')] = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . '<br>';
  145. //系统运行时间
  146. $query_time = number_format((microtime(true) - StartTime), 3) . 's';
  147. $trace[Language::get('nc_debug_execution_time')] = $query_time . '<br>';
  148. //内存
  149. $trace[Language::get('nc_debug_memory_consumption')] = number_format(memory_get_usage() / 1024 / 1024, 2) . 'MB' . '<br>';
  150. //请求方法
  151. $trace[Language::get('nc_debug_request_method')] = $_SERVER['REQUEST_METHOD'] . '<br>';
  152. //通信协议
  153. $trace[Language::get('nc_debug_communication_protocol')] = $_SERVER['SERVER_PROTOCOL'] . '<br>';
  154. //用户代理
  155. $trace[Language::get('nc_debug_user_agent')] = $_SERVER['HTTP_USER_AGENT'] . '<br>';
  156. //会话ID
  157. $trace[Language::get('nc_debug_session_id')] = session_id() . '<br>';
  158. //执行日志
  159. $log = Log::read();
  160. $trace[Language::get('nc_debug_logging')] = count($log) ? count($log) . Language::get('nc_debug_logging_1') . '<br/>' . implode('<br/>', $log) : Language::get('nc_debug_logging_2');
  161. $trace[Language::get('nc_debug_logging')] = $trace[Language::get('nc_debug_logging')] . '<br>';
  162. //文件加载
  163. $files = get_included_files();
  164. $trace[Language::get('nc_debug_load_files')] = count($files) . str_replace("\n", '<br/>', substr(substr(print_r($files, true), 7), 0, -2)) . '<br>';
  165. return $trace;
  166. }
  167. }