最近苏州网站建设接手制作了一个网站,在使用pbootcms网站后台的时候发现,客户给的栏目层级架构有点太深了,一直能达到6级分类,这让我在做产品列表左侧分类选中的时候为难了,pbootcms在官方手册里当前栏目标签中,获取当前栏目的顶级栏目编码->{sort:tcode}、获取当前栏目的父栏目编码->{sort:pcode}、获取当前栏目编码->{sort:scode},当我要获取到产品列表里面当前位置的第二个位置的栏目id的时候那就没办法获取了,没办法只能参照这获取当前位置的源码来改动,生成适合我使用的标签了。以下是我的一些思路:
在ExtLabelController控制器中编写个人扩展标签:
1、是使用正则表达是匹配替换出当前模板中是否存在需要替换的标签内容,正则表达是为:
{pboot:pre}
$pattern = '/{pboot:positionarr(s+[^}]+)?}/';{/pboot:pre}
2、将当前内容赋值给一个变量:
{pboot:pre}
$content = $this->content;
{/pboot:pre}
3、判断当前模板内容中使用正则匹配出需要查找的内容:
{pboot:pre}
if (preg_match_all($pattern, $this->content, $matches)) {}{/pboot:pre}
4、获取调节参数
{pboot:pre}
// 获取调节参数 $params = $this->parserParam($matches[1][$i]);
{/pboot:pre}
获取调节参数的方法是私有方法,从ParserController中复制出一下代码到当前的个人扩展标签中:
{pboot:pre}
// 解析调节参数
protected function parserParam($string, $striptags = true)
{
if (! $string = trim($string))
return array();
$string = preg_replace('/s+/', ' ', $string); // 多空格处理
if ($striptags) {
$string = strip_tags($string);
}
$param = array();
if (preg_match_all('/([w]+)[s]?=[s]?(["]([^"]+)?["]|[']([^']+)?[']|([^s]+))/i', $string, $matches)) {
foreach ($matches[1] as $key => $value) {
$param[$value] = $matches[3][$key] ?: $matches[4][$key] ?: $matches[5][$key];
}
}
return $param;
}{/pboot:pre}
5、判断获取调节参数中是否存在变量scode:
{pboot:pre}
if(array_key_exists('scode',$params)){
$scode = $params['scode'];
}else{
$scode = 0;
}{/pboot:pre}
6、判断获取调节参数中需要返回当前位置的第几个参数:
{pboot:pre}
//返回数组第几个
if(array_key_exists('rnum',$params)){
$rnum = $params['rnum'];
}else{
$rnum = 0;
}{/pboot:pre}
7、获取指定栏目scode的当前位置:
{pboot:pre}
$model = new ParserModel(); $data = $model->getPosition($scode);
{/pboot:pre}
8、字符串替换将模板中的标签替换成当前位置返回值:
{pboot:pre}
$content = str_replace($matches[0][$i],$data[$rnum]['scode'],$content);
{/pboot:pre}
至此一个完整的思路已经完成了。
接下来就是完整的代码:
{pboot:pre}
use apphomemodelParserModel;
use coreasicController;
class ExtLabelController
{
protected $content;
/* 必备启动函数 */
public function run($content)
{
// 接收数据
$this->content = $content;
// 执行个人自定义标签函数
$this->test();
$this->getPosition();
// 返回数据
return $this->content;
}
// 测试扩展单个标签
private function test()
{
$this->content = str_replace('{pboot:userip}', get_user_ip(), $this->content);
}
/**
* 当前位置指定返回那一层父级ID
* Author: Wusn <958342972@qq.com>
* DateTime: 2022/3/25 3:10 下午
*/
private function getPosition(){
$pattern = '/{pboot:positionarr(s+[^}]+)?}/';
$model = new ParserModel();
$content = $this->content;
if (preg_match_all($pattern, $this->content, $matches)) {
// $this->dump($matches);
$count = count($matches[0]);
for ($i = 0; $i < $count; $i ++) {
// 获取调节参数
$params = $this->parserParam($matches[1][$i]);
if(array_key_exists('scode',$params)){
$scode = $params['scode'];
}else{
$scode = 0;
}
//返回数组第几个
if(array_key_exists('rnum',$params)){
$rnum = $params['rnum'];
}else{
$rnum = 0;
}
$data = $model->getPosition($scode);
$content = str_replace($matches[0][$i],$data[$rnum]['scode'],$content);
}
}
$this->content = $content;
}
// 解析调节参数
protected function parserParam($string, $striptags = true)
{
if (! $string = trim($string))
return array();
$string = preg_replace('/s+/', ' ', $string); // 多空格处理
if ($striptags) {
$string = strip_tags($string);
}
$param = array();
if (preg_match_all('/([w]+)[s]?=[s]?(["]([^"]+)?["]|[']([^']+)?[']|([^s]+))/i', $string, $matches)) {
foreach ($matches[1] as $key => $value) {
$param[$value] = $matches[3][$key] ?: $matches[4][$key] ?: $matches[5][$key];
}
}
return $param;
}
/**
* 数据格式化
* @param $content
* Author: Wusn <958342972@qq.com>
* DateTime: 2022/3/25 2:54 下午
*/
private function dump($content){
echo "<pre>";
print_r($content);
echo "</pre>";
}
}{/pboot:pre}











