Tp.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\taglib;
  4. use think\template\TagLib;
  5. /**
  6. * 标签扩展
  7. */
  8. class Tp extends TagLib
  9. {
  10. /**
  11. * 标签定义
  12. * attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次
  13. * @var array
  14. */
  15. protected $tags = array(
  16. 'close' => ['attr' => 'time,format', 'close' => 0], // 闭合标签,默认为不闭合
  17. 'open' => ['attr' => 'name,type', 'close' => 1],
  18. 'nav' => ['attr' => 'id,limit,name', 'close' => 1], // 通用导航信息
  19. // 'cate' => ['attr' => 'id,type,anchor', 'close' => 0], // 通用栏目信息
  20. // 'position' => ['attr' => 'name', 'close' => 1], // 通用位置信息
  21. // 'link' => ['attr' => 'name', 'close' => 1], // 获取友情链接
  22. // 'ad' => ['attr' => 'name,id', 'close' => 1], // 获取广告信息
  23. 'listbycid' => ['attr' => 'cid,name,limit', 'close' => 1], // 通用列表
  24. 'listtime' => ['attr' => 'name,limit', 'close' => 1], // 时间归档
  25. );
  26. // 这是一个闭合标签的简单演示
  27. public function tagClose($tag)
  28. {
  29. $format = empty($tag['format']) ? 'Y-m-d H:i:s' : $tag['format'];
  30. $time = empty($tag['time']) ? time() : $tag['time'];
  31. $parse = '<?php ';
  32. $parse .= 'echo date("' . $format . '",' . $time . ');';
  33. $parse .= ' ?>';
  34. return $parse;
  35. }
  36. // 这是一个非闭合标签的简单演示
  37. public function tagOpen($tag, $content)
  38. {
  39. $type = empty($tag['type']) ? 0 : 1; // 这个type目的是为了区分类型,一般来源是数据库
  40. $name = $tag['name']; // name是必填项,这里不做判断了
  41. $parse = '<?php ';
  42. $parse .= '$test_arr=[[1,3,5,7,9],[2,4,6,8,10]];'; // 这里是模拟数据
  43. $parse .= '$__LIST__ = $test_arr[' . $type . '];';
  44. $parse .= ' ?>';
  45. $parse .= '{volist name="__LIST__" id="' . $name . '"}';
  46. $parse .= $content;
  47. $parse .= '{/volist}';
  48. return $parse;
  49. }
  50. // 文章列表
  51. public function tagListbycid($tag, $content)
  52. {
  53. $cid = (int) $tag['cid']; // 不可以为空
  54. $name = (string) $tag['name']; // 不可为空
  55. $order = isset($tag['order']) ? (string) $tag['order'] : 'sort ASC,id DESC'; // 可为空
  56. $limit = $tag['limit'] ? (int) $tag['limit'] : 10; // 多少条数据
  57. $parse = '<?php ';
  58. $parse .= '$list = \app\model\Article::getListByCid(' . $cid . ', ' . $limit . ', "' . $order .'");';
  59. $parse .= '?>';
  60. $parse .= '{volist name="list" id="' . $name . '"}';
  61. $parse .= $content;
  62. $parse .= '{/volist}';
  63. return $parse;
  64. }
  65. //
  66. public function tagListtime($tag, $content)
  67. {
  68. $name = (string) $tag['name']; // 不可为空
  69. $limit = $tag['limit'] ? (int) $tag['limit'] : 0; // 多少条数据 0 不限制
  70. $parse = '<?php ';
  71. $parse .= '$list = \app\model\Article::createTimeArchive(' . $limit . ');';
  72. $parse .= '?>';
  73. $parse .= '{volist name="list" id="' . $name . '"}';
  74. $parse .= $content;
  75. $parse .= '{/volist}';
  76. return $parse;
  77. }
  78. }