view.init.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. if (! defined('IS_INITPHP'))
  3. exit('Access Denied!');
  4. /**
  5. * *******************************************************************************
  6. * InitPHP 3.8.1 国产PHP开发框架 View-view 模板核心文件类
  7. * -------------------------------------------------------------------------------
  8. * 版权所有: CopyRight By initphp.com
  9. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  10. * -------------------------------------------------------------------------------
  11. * Author:zhuli Dtime:2014-11-25
  12. * *********************************************************************************
  13. */
  14. class templateInit {
  15. private $template_path = 'template';
  16. // 模板目录
  17. private $template_c_path = 'template_c';
  18. // 编译目录
  19. private $template_type = 'htm';
  20. // 模板文件类型
  21. private $template_c_type = 'tpl.php';
  22. // 模板编译文件类型
  23. private $template_tag_left = '<!--{';
  24. // 左标签
  25. private $template_tag_right = '}-->';
  26. // 右标签
  27. private $is_compile = true;
  28. // 是否需要每次编译
  29. private $driver_config;
  30. private static $driver = NULL;
  31. // 定义默认的一个模板编译驱动模型
  32. /**
  33. * 模板编译-设置模板信息
  34. * Controller中使用方法:$this->view->set_template_config($config)
  35. *
  36. * @param array $config
  37. * 设置参数
  38. * @return bool
  39. */
  40. public function set_template_config($config) {
  41. if (! is_array($config))
  42. return false;
  43. $config['template_path'] = InitPHP::getAppPath($config['template_path']);
  44. $config['template_c_path'] = InitPHP::getAppPath($config['template_c_path']);
  45. if (isset($config['theme']) && ! empty($config['theme'])) { // 模板主题实现
  46. $config['template_path'] = $config['template_path'] . '/' . $config['theme'];
  47. $config['template_c_path'] = $config['template_c_path'] . '/' . $config['theme'];
  48. if ($config['is_compile'] == true)
  49. $this->create_dir($config['template_c_path']); // 创建主题文件夹
  50. }
  51. if (isset($config['template_path']))
  52. $this->template_path = $config['template_path'];
  53. if (isset($config['template_c_path']))
  54. $this->template_c_path = $config['template_c_path'];
  55. if (isset($config['template_type']))
  56. $this->template_type = $config['template_type'];
  57. if (isset($config['template_c_type']))
  58. $this->template_c_type = $config['template_c_type'];
  59. if (isset($config['template_tag_left']))
  60. $this->template_tag_left = $config['template_tag_left'];
  61. if (isset($config['template_tag_right']))
  62. $this->template_tag_right = $config['template_tag_right'];
  63. if (isset($config['is_compile']))
  64. $this->is_compile = $config['is_compile'];
  65. $this->driver_config = $config['driver'];
  66. return true;
  67. }
  68. /**
  69. * 模板编译-模板类入口函数
  70. * 1.
  71. * 获取模板,如果模板未编译,则编译
  72. *
  73. * @param string $filename
  74. * 文件名称,例如:test,不带文件.htm类型
  75. * @return string
  76. */
  77. protected function template_run($file_name) {
  78. $this->check_path(); // 检测模板目录和编译目录
  79. list ($template_file_name, $compile_file_name) = $this->get_file_name($file_name);
  80. if (($this->is_compile == true) || ($this->is_compile == false && ! file_exists($compile_file_name))) { // 是否强制编译
  81. $str = $this->read_template($template_file_name);
  82. $str = $this->layout($str); // layout模板页面中加载模板页
  83. $str = $this->replace_tag($str);
  84. $str = $this->compile_version($str, $template_file_name);
  85. $this->compile_template($compile_file_name, $str);
  86. }
  87. return $compile_file_name;
  88. }
  89. /**
  90. * 模板编译-读取静态模板
  91. *
  92. * @param string $filename
  93. * 文件名称,例如:test,不带文件.htm类型
  94. * @return
  95. *
  96. */
  97. private function read_template($template_file_name) {
  98. if (! file_exists($template_file_name))
  99. InitPHP::initError($template_file_name . ' is not exist!');
  100. return @file_get_contents($template_file_name);
  101. }
  102. /**
  103. * 模板编译-编译模板
  104. *
  105. * @param string $filename
  106. * 文件名称,例如:test,不带文件.htm类型
  107. * @param string $str
  108. * 写入编译文件的数据
  109. * @return
  110. *
  111. */
  112. private function compile_template($compile_file_name, $str) {
  113. if (($path = dirname($compile_file_name)) !== $this->template_c_path) { // 自动创建文件夹
  114. $this->create_dir($path);
  115. }
  116. $ret = @file_put_contents($compile_file_name, $str);
  117. if ($ret == false) {
  118. InitPHP::initError("Please check the Directory have read/write permissions. If it's not, please set 777 limits. Can not write " . $compile_file_name);
  119. }
  120. }
  121. /**
  122. * 模板编译-通过传入的filename,获取要编译的静态页面和生成编译文件的文件名
  123. *
  124. * @param string $filename
  125. * 文件名称,例如:test,不带文件.htm类型
  126. * @return
  127. *
  128. */
  129. private function get_file_name($file_name) {
  130. return array(
  131. $this->template_path . '/' . $file_name . '.' . $this->template_type, // 组装模板文件路径
  132. $this->template_c_path . '/' . $file_name . '.' . $this->template_c_type
  133. ) // 模板编译路径
  134. ;
  135. }
  136. /**
  137. * 模板编译-检测模板目录和编译目录是否可写
  138. *
  139. * @return
  140. *
  141. */
  142. private function check_path() {
  143. if (! is_dir($this->template_path) || ! is_readable($this->template_path))
  144. InitPHP::initError('template path is unread!');
  145. if (! is_dir($this->template_c_path) || ! is_readable($this->template_c_path))
  146. InitPHP::initError('compiled path is unread!');
  147. return true;
  148. }
  149. /**
  150. * 模板编译-编译文件-头部版本信息
  151. *
  152. * @param string $str
  153. * 模板文件数据
  154. * @return string
  155. */
  156. private function compile_version($str, $template_file_name) {
  157. $version_str = '<?php if (!defined("IS_INITPHP")) exit("Access Denied!"); /* INITPHP Version 1.0 ,Create on ' . date('Y-m-d H:i:s');
  158. $version_str .= ', compiled from ' . $template_file_name . ' */ ?>' . "\r\n";
  159. return $version_str . $str;
  160. }
  161. /**
  162. * 模板编译-标签正则替换
  163. *
  164. * @param string $str
  165. * 模板文件数据
  166. * @return string
  167. */
  168. private function replace_tag($str) {
  169. $this->get_driver($this->driver_config);
  170. return self::$driver->init($str, $this->template_tag_left, $this->template_tag_right); // 编译
  171. }
  172. /**
  173. * 模板编译-layout 模板layout加载机制
  174. * 1.
  175. * 在HTML模板中直接使用<!--{layout:user/version}-->就可以调用模板
  176. *
  177. * @param string $str
  178. * 模板文件数据
  179. * @return string
  180. */
  181. private function layout($str) {
  182. preg_match_all("/(" . $this->template_tag_left . "layout:)(.*)(" . $this->template_tag_right . ")/", $str, $matches);
  183. $matches[2] = array_unique($matches[2]); // 重复值移除
  184. $matches[0] = array_unique($matches[0]);
  185. foreach ($matches[2] as $val)
  186. $this->template_run($val);
  187. foreach ($matches[0] as $k => $v) {
  188. $str = str_replace($v, $this->layout_path($matches[2][$k]), $str);
  189. }
  190. return $str;
  191. }
  192. /**
  193. * 模板编译-layout路径
  194. *
  195. * @param string $template_name
  196. * 模板名称
  197. * @return string
  198. */
  199. private function layout_path($template_name) {
  200. return "<?php include('" . $this->template_c_path . '/' . $template_name . '.' . $this->template_c_type . "'); ?>";
  201. }
  202. /**
  203. * 模板编译-获取不同
  204. *
  205. * @param string $template_name
  206. * 模板名称
  207. * @return string
  208. */
  209. private function get_driver($driver) {
  210. $diver_path = 'driver/' . $driver . '.init.php';
  211. if (self::$driver === NULL) {
  212. require_once ($diver_path);
  213. $class = $driver . 'Init';
  214. if (! class_exists($class))
  215. InitPHP::initError('class' . $class . ' is not exist!');
  216. $init_class = new $class();
  217. self::$driver = $init_class;
  218. }
  219. return self::$driver;
  220. }
  221. /**
  222. * 创建目录
  223. *
  224. * @param string $path
  225. * 目录
  226. * @return
  227. *
  228. */
  229. private function create_dir($path) {
  230. if (is_dir($path))
  231. return false;
  232. $this->create_dir(dirname($path));
  233. @mkdir($path);
  234. @chmod($path, 0777);
  235. return true;
  236. }
  237. }
  238. class viewInit extends templateInit {
  239. public $view = array();
  240. // 视图变量
  241. private $template_arr = array();
  242. // 视图存放器
  243. private $remove_tpl_arr = array();
  244. // 待移除
  245. /**
  246. * 模板-设置模板变量-将PHP中是变量输出到模板上,都需要经过此函数
  247. * 1.
  248. * 模板赋值核心函数,$key => $value , 模板变量名称 => 控制器中变量
  249. * 2. 也可以直接用$this->view->view['key'] = $value
  250. * 3. 模板中对应$key
  251. * Controller中使用方法:$this->view->assign($key, $value);
  252. *
  253. * @param string $key
  254. * KEY值-模板中的变量名称
  255. * @param string|array $value
  256. * value值
  257. * @return void
  258. */
  259. public function assign($key, $value) {
  260. $this->view[$key] = $value;
  261. }
  262. /**
  263. * 模板-设置模板 设置HTML模板
  264. * 1.
  265. * 设置模板,模板名称和类型,类型选择F和L的时候,是最先显示和最后显示的模板
  266. * 2. 比如设置 user目录下的userinfo.htm目录,则 set_tpl('user/userinfo') 不需要填写.htm
  267. * Controller中使用方法:$this->view->set_tpl($template_name, $type = '');
  268. *
  269. * @param string $template_name
  270. * 模板名称
  271. * @param string $type
  272. * 类型,F-头模板,L-脚步模板
  273. * @return array
  274. */
  275. public function set_tpl($template_name, $type = '') {
  276. if ($type == 'F') {
  277. $this->template_arr['F'] = $template_name;
  278. } elseif ($type == 'L') {
  279. $this->template_arr['L'] = $template_name;
  280. } else {
  281. $this->template_arr[] = $template_name;
  282. }
  283. }
  284. /**
  285. * 模板-移除模板
  286. * 1.
  287. * 如果在控制器的基类中已经导入头部和脚步模板,应用中需要替换头部模板
  288. * 2. 移除模板需要在display() 模板显示前使用
  289. * Controller中使用方法:$this->view->remove_tpl($remove_tpl);
  290. *
  291. * @param string $remove_tpl
  292. * 需要移除模板名称
  293. * @return array
  294. */
  295. public function remove_tpl($remove_tpl) {
  296. $this->remove_tpl_arr[] = $remove_tpl;
  297. }
  298. /**
  299. * 模板-获取模板数组
  300. * Controller中使用方法:$this->view->get_tpl();
  301. *
  302. * @return array
  303. */
  304. public function get_tpl() {
  305. return $this->template_arr;
  306. }
  307. /**
  308. * 模板-显示视图
  309. * 1.
  310. * 在Controller中需要显示模板,就必须调用该函数
  311. * 2. 模板解析可以设置 $InitPHP_conf['isviewfilter'] 值,对变量进行过滤
  312. * Controller中使用方法:$this->view->display();
  313. *
  314. * @return array
  315. */
  316. public function display($template = '', $isfilter = TRUE) {
  317. if ($template != '') {
  318. $this->set_tpl($template);
  319. }
  320. $InitPHP_conf = InitPHP::getConfig();
  321. if (is_array($this->view)) {
  322. if ($isfilter) {
  323. if ($InitPHP_conf['isviewfilter'])
  324. $this->out_put($this->view);
  325. }
  326. foreach ($this->view as $key => $val) {
  327. $$key = $val;
  328. }
  329. }
  330. $this->template_arr = $this->parse_template_arr($this->template_arr); // 模板设置
  331. foreach ($this->template_arr as $file_name) {
  332. if (in_array($file_name, $this->remove_tpl_arr))
  333. continue;
  334. $complie_file_name = $this->template_run($file_name); // 模板编译
  335. if (! file_exists($complie_file_name))
  336. InitPHP::initError($complie_file_name . ' is not exist!');
  337. include_once ($complie_file_name);
  338. }
  339. }
  340. /**
  341. * 模板-处理视图存放器数组,分离头模板和脚模板顺序
  342. *
  343. * @param array $arr
  344. * 视图存放器数组
  345. * @return array
  346. */
  347. private function parse_template_arr(array $arr) {
  348. $temp = $arr;
  349. unset($temp['F'], $temp['L']);
  350. if (isset($this->template_arr['F'])) { // 头模板
  351. array_unshift($temp, $this->template_arr['F']);
  352. }
  353. if (isset($this->template_arr['L'])) {
  354. array_push($temp, $this->template_arr['L']);
  355. }
  356. return $temp;
  357. }
  358. /**
  359. * 模板-模板变量输出过滤
  360. *
  361. * @param array $arr
  362. * 视图存放器数组
  363. * @return array
  364. */
  365. private function out_put(&$value) {
  366. $value = (array) $value;
  367. foreach ($value as $key => $val) {
  368. if (is_array($val)) {
  369. self::out_put($value[$key]);
  370. } elseif (is_object($val)) {
  371. $value[$key] = $val;
  372. } else {
  373. if (function_exists('htmlspecialchars')) {
  374. $value[$key] = htmlspecialchars($val);
  375. } else {
  376. $value[$key] = str_replace(array(
  377. "&",
  378. '"',
  379. "'",
  380. "<",
  381. ">",
  382. "%3C",
  383. "%3E"
  384. ), array(
  385. "&amp;",
  386. "&quot;",
  387. "&#039;",
  388. "&lt;",
  389. "&gt;",
  390. "&lt;",
  391. "&gt;"
  392. ), $val);
  393. }
  394. }
  395. }
  396. }
  397. }