tpl.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /**
  78. * 调用显示模板
  79. *
  80. * @param string $page_name
  81. * @param string $layout
  82. * @param int $time
  83. */
  84. public static function showpage($page_name = '', $layout = '', $time = 2000)
  85. {
  86. if (!defined('TPL_NAME')) define('TPL_NAME', 'default');
  87. self::getInstance();
  88. if (!empty(self::$tpl_dir)) {
  89. $tpl_dir = self::$tpl_dir . DS;
  90. }
  91. //默认是带有布局文件
  92. if (empty($layout)) {
  93. $layout = 'layout' . DS . self::$layout_file . '.php';
  94. } else {
  95. $layout = 'layout' . DS . $layout . '.php';
  96. }
  97. $layout_file = BASE_PATH . '/templates/' . TPL_NAME . DS . $layout;
  98. $tpl_file = BASE_PATH . '/templates/' . TPL_NAME . DS . $tpl_dir . $page_name . '.php';
  99. if (file_exists($tpl_file))
  100. {
  101. //对模板变量进行赋值
  102. $output = self::$output_value;
  103. //页头
  104. $output['html_title'] = $output['html_title'] != '' ? $output['html_title'] : $GLOBALS['setting_config']['site_name'];
  105. $output['seo_keywords'] = $output['seo_keywords'] != '' ? $output['seo_keywords'] : $GLOBALS['setting_config']['site_name'];
  106. $output['seo_description'] = $output['seo_description'] != '' ? $output['seo_description'] : $GLOBALS['setting_config']['site_name'];
  107. $output['ref_url'] = getReferer();
  108. Language::read('common');
  109. $lang = Language::getLangContent();
  110. @header("Content-type: text/html; charset=" . CHARSET);
  111. //判断是否使用布局方式输出模板,如果是,那么包含布局文件,并且在布局文件中包含模板文件
  112. if ($layout != '')
  113. {
  114. if (file_exists($layout_file)) {
  115. include_once($layout_file);
  116. } else {
  117. $error = 'Tpl ERROR:' . 'templates' . DS . $layout . ' is not exists';
  118. throw_exception($error);
  119. }
  120. } else {
  121. include_once($tpl_file);
  122. }
  123. } else {
  124. $error = 'Tpl ERROR:' . 'templates' . DS . $tpl_dir . $page_name . '.php' . ' is not exists';
  125. throw_exception($error);
  126. }
  127. }
  128. /**
  129. * 显示页面Trace信息
  130. *
  131. * @return array
  132. */
  133. public static function showTrace()
  134. {
  135. $trace = array();
  136. //当前页面
  137. $trace[Language::get('nc_debug_current_page')] = $_SERVER['REQUEST_URI'] . '<br>';
  138. //请求时间
  139. $trace[Language::get('nc_debug_request_time')] = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . '<br>';
  140. //系统运行时间
  141. $query_time = number_format((microtime(true) - StartTime), 3) . 's';
  142. $trace[Language::get('nc_debug_execution_time')] = $query_time . '<br>';
  143. //内存
  144. $trace[Language::get('nc_debug_memory_consumption')] = number_format(memory_get_usage() / 1024 / 1024, 2) . 'MB' . '<br>';
  145. //请求方法
  146. $trace[Language::get('nc_debug_request_method')] = $_SERVER['REQUEST_METHOD'] . '<br>';
  147. //通信协议
  148. $trace[Language::get('nc_debug_communication_protocol')] = $_SERVER['SERVER_PROTOCOL'] . '<br>';
  149. //用户代理
  150. $trace[Language::get('nc_debug_user_agent')] = $_SERVER['HTTP_USER_AGENT'] . '<br>';
  151. //会话ID
  152. $trace[Language::get('nc_debug_session_id')] = session_id() . '<br>';
  153. //执行日志
  154. $log = Log::read();
  155. $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');
  156. $trace[Language::get('nc_debug_logging')] = $trace[Language::get('nc_debug_logging')] . '<br>';
  157. //文件加载
  158. $files = get_included_files();
  159. $trace[Language::get('nc_debug_load_files')] = count($files) . str_replace("\n", '<br/>', substr(substr(print_r($files, true), 7), 0, -2)) . '<br>';
  160. return $trace;
  161. }
  162. }