为什么写这个?可能有人觉得没啥作用,我写这个的目的就是为了让pbootcms更适合自己使用,更简洁简便。
2020.04.04 新增智能路径,js、css自动追加版本号;快速链接。
2020.04.03 新增每日一图标签。
每日一图
调用必应搜索的每日一图接口,返回图片URL,调用标签:{ pboot:walle } //这里自己去掉空格哦。
快速链接
举例:pbootcms调用某个栏目的超链接的时候,需要这样写:
{pboot:sort scode=1}
<a href= "[sort:link]">关于我们</a>
{/pboot:sort}使用了快速链接扩展,只需要这样写:
//下面没空格的,突然发现标签被解析了..所以我加了个空格看着方便
{ @about.1 }//点后面跟scode的id
{ @list.2 }//点后面跟scode的id
{ @content.3 }//点后面跟文章的id智能路径
pbootcms默认有 {pboot:sitetplpath} 这个标签,每次写这个都很麻烦。
所以需要让他更智能一些。
智能路径会自动补全模板中的src=|href=|value=|background=这些标记后面的内容,当然并不是全部替换,只自动补全images|img|css|js|style这几个文件夹的路径。
举个例子:
<link rel="stylesheet" type="text/css" href="css/common.css"> <script src="js/common.js"></script> <img src="images/logo.png" />
然后就会自动补足路径,js和css会自动追加版本号
最终效果如下:
<link rel="stylesheet" type="text/css" href="/template/a8yun/css/common.css?v=20200404134138"> <script src="/template/a8yun/js/common.js?v=20200403155213"></script> <img src="/template/a8yun/images/logo.png" />
这一切都是系统自动完成的。
有人会说这样写模板会被偷的,我只能说扣码防不住。
下面贴上完整的ExtLabelController.php代码,pbootcms v2的用户只需拷贝到apps/home/controller/ExtLabelController.php这个文件下即可。
<?php
/**
* @author 9029855@qq.com
*/
namespace app\home\controller;
use core\basic\Controller;
use core\basic\Url;
use app\home\model/ParserModel;
class ExtLabelController extends Controller
{
protected $content;
protected $model;
public function __construct()
{
$this->model = new ParserModel();
}
/* 必备启动函数 */
public function run($content)
{
// 接收数据
$this->content = $content;
// 执行个人自定义标签函数
$this->diylabel();
//快速标签
$this->fastUrl();
//智能模板路径、自动更新CSS,JS版本号,不用再让客户强制刷新啦
$this->smartURL();
// 返回数据
return $this->content;
}
// 扩展单个标签
private function diylabel()
{
//{ pboot:walle } 每日一图的图片URL //这里自己去掉空格哦。
$this->content = str_replace('{ pboot:walle }', $this->getBingImage(), $this->content);
}
//抓取必应每日一图
private function getBingImage(){
$url = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1';
$data = json_decode(get_url($url));
$result = $data->images[0];
$image = 'https://www.bing.com'.$result->url;
return $image;
}
//解析快速URL{ @about.1 }{ @list.2 }{ @content.3 } 为啥要用@,因为$用不了了,咱们就用@召唤神兽吧 括号中无空格哦
private function fastUrl(){
$url_break_char = $this->config('url_break_char') ?: '_';
$url_rule_suffix = $this->config('url_rule_suffix') ?: '.html';
$url_rule_sort_suffix = $this->config('url_rule_sort_suffix') ? $url_rule_suffix : '/';
$pattern = '/{@(about|list|content).([a-z0-9_]+)}/';
if (preg_match($pattern, $this->content, $matches)) {
$this->content = preg_replace_callback(
$pattern,
function($matches) use ( $url_break_char, $url_rule_suffix, $url_rule_sort_suffix ){
switch ($matches[1]){
case 'about';
$data = $this->model->getAbout($matches[2]);
$data->urlname = $data->urlname ?: 'about';
if ($data->sortfilename) {
$link = Url::home($data->sortfilename, $url_rule_sort_suffix);
} else {
$link = Url::home($data->urlname . $url_break_char . $data->scode, $url_rule_sort_suffix);
}
return $link;
break;
case 'list';
$data = $this->model->getSort($matches[2]);
$data->urlname = $data->urlname ?: 'list';
if ($data->filename) {
$link = Url::home($data->filename, $url_rule_sort_suffix);
} else {
$link = Url::home($data->urlname . $url_break_char . $data->scode, $url_rule_sort_suffix);
}
return $link;
break;
case 'content';
$data = $this->model->getContent($matches[2]);
$data->urlname = $data->urlname ?: 'list';
if ($data->sortfilename && $data->filename) {
$link = Url::home($data->sortfilename . '/' . $data->filename, true);
} elseif ($data->sortfilename) {
$link = Url::home($data->sortfilename . '/' . $data->id, true);
} elseif ($data->filename) {
$link = Url::home($data->urlname . $url_break_char . $data->scode . '/' . $data->filename, true);
} else {
$link = Url::home($data->urlname . $url_break_char . $data->scode . '/' . $data->id, true);
}
return $link;
break;
}
},
$this->content);
}
}
//智能路径
private function smartURL(){
$pattern = '/<(.*?)(src=|href=|value=|background=)["|'](images/|img/|css/|js/|style/)(.*?)["|'](.*?)>/';
if (preg_match($pattern, $this->content, $matches)) {
$this->content = preg_replace_callback(
$pattern,
function($matches){
if( strstr($matches[4], '.js') || strstr($matches[4],'.css') ){
return '<'.$matches[1].$matches[2].'"'.$this->auto_version(APP_THEME_DIR.'/'.$matches[3].$matches[4]).'"'. $matches[5] .'>';
}else{
return '<'.$matches[1].$matches[2].'"'.APP_THEME_DIR.'/'.$matches[3].$matches[4].'"'. $matches[5] .'>';
}
},
$this->content);
}
}
//自动更新时间版本号
public function auto_version($url){
$ver = filemtime(DOC_PATH . $url);
return $url.'?v='.date("YmdHis",$ver);
}
}本文不定期更新。
CMS博客 原创,未经授权禁止转载、摘编、复制或建立镜像。
文章评论(审核通过可见)