TemplateTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 03/03/2017
  6. * Time: 12:21
  7. */
  8. namespace app\common\traits;
  9. use app\common\helpers\Url;
  10. use app\common\helpers\StringHelper;
  11. trait TemplateTrait
  12. {
  13. //当前模块名数组
  14. public $modules = [];
  15. //当前控制器
  16. public $controller = '';
  17. //当前action
  18. public $action = '';
  19. //当前路由
  20. public $route = '';
  21. public $title = '';
  22. public $breadcrumbs = [];
  23. /**
  24. * 渲染视图
  25. *
  26. * ```php
  27. * $this->render('index',['list'=>$list]);
  28. *
  29. * 模板名可直接写 当前action名,方法会自动补全路径,如果名称里有/则不补全
  30. *
  31. * ```
  32. * @param $filename 模板名
  33. * @param array $data 模板变量
  34. * @return mixed
  35. */
  36. public function render($filename, $data = [])
  37. {
  38. if (strpos($filename, '/') === false) {
  39. $this->controller && $filename = strtolower(StringHelper::camelCaseToSplit($this->controller)) . '/' . $filename;
  40. $this->modules && $filename = StringHelper::camelCaseToSplit(implode('/', $this->modules)) . '/' . $filename;
  41. }
  42. $dataVar = ['var' => objectArray(\YunShop::app()), 'request' => (\YunShop::request())];
  43. is_array($data) && $dataVar = array_merge($data, $dataVar);
  44. extract($dataVar);
  45. $var =array_shift($var);
  46. /*$request =array_shift($request);*/
  47. include $this->template($filename, $data);
  48. return ;
  49. }
  50. /**
  51. * 编译并获取模板路径
  52. * @param $filename
  53. * @return string
  54. */
  55. public function template($filename)
  56. {
  57. strpos($filename, 'web/') !== false && $filename = str_replace('web/', '', $filename);
  58. $compile = base_path() . "/data/tpl/{$filename}.tpl.php";
  59. $source = base_path() . "/template/web/{$filename}.html";
  60. if (DEVELOPMENT || !is_file($compile) || filemtime($source) > filemtime($compile)) {
  61. shop_template_compile($source, $compile, true);
  62. }
  63. return $compile;
  64. }
  65. public function viewWebUrl($url, $params = [])
  66. {
  67. if(empty($url)){
  68. return 'javascript:void(0)';
  69. }
  70. if(strpos($url, 'http://') === 0 ||
  71. strpos($url, 'https://') === 0 ||
  72. strpos($url, '/web/') === 0){
  73. return $url;
  74. }
  75. return $this->createWebUrl($url, is_array($params) ? $params : []);
  76. }
  77. /**
  78. * 生成后台url
  79. * @param $route 路由
  80. * @param $params 参数
  81. * @return string
  82. */
  83. public function createWebUrl($route, $params = [])
  84. {
  85. return Url::absoluteWeb($route, $params);
  86. }
  87. /**
  88. * 生成插件url
  89. * @param $route 路由
  90. * @param $params 参数
  91. * @return string
  92. */
  93. public function createPluginWebUrl($route, $params = [])
  94. {
  95. return Url::absoluteWeb($route, $params);
  96. }
  97. /**
  98. * 生成插件url
  99. * @param $route 路由
  100. * @param $params 参数
  101. * @return string
  102. */
  103. public function createPluginMobileUrl($route, $params = [])
  104. {
  105. return Url::absoluteApp($route, $params);
  106. }
  107. /**
  108. * 生成前台Url
  109. * @param $route 路由
  110. * @param $params 参数
  111. * @return string
  112. */
  113. public function createMobileUrl($route, $params = [])
  114. {
  115. return Url::absoluteApp($route, $params);
  116. }
  117. }