thinkphp6自定義標(biāo)簽
public function tagTitle()
{
$parse = '<?php ';
$parse .= '$tagChannel = new app\common\taglib\ht\tagTitle;';
$parse .= '$data = $tagChannel->createTitle();';
$parse .= 'echo $data;';
$parse .= 'unset($data)';
$parse .= ' ?>';
return $parse;
}public function createTitle()
{
$t = '';
$k = '';
$d = '';
$Config = cache("Config");
$aid = input('param.aid/d', '');
$tid = input('param.tid/d', '');
$pid = input('param.pid/d', '');
$title = "<title>%s</title>\n\t<meta name=\"description\" content=\"%s\"/>\n\t<meta name=\"keywords\" content=\"%s\"/>\n";
if (empty($aid) && empty($tid) && empty($pid)) { // 都為空 認(rèn)為是首頁(yè)
$t = !empty($Config["web_title"]) ? $Config["web_title"] : $Config["web_name"];
$k = !empty($Config["web_keywords"]) ? $Config["web_keywords"] : $Config["web_name"];
$d = !empty($Config["web_description"]) ? $Config["web_description"] : $Config["web_name"];
}
if (!empty($aid)) { // 內(nèi)容頁(yè)
$tagarticle = Archives::find($aid);
$t = !empty($tagarticle['title']) ? $tagarticle['title'] . "-" . $Config["web_name"] : $Config["web_name"];
$k = !empty($tagarticle['seo_key']) ? $tagarticle['seo_key'] : $Config["web_keywords"];
$d = !empty($tagarticle['seo_des']) ? $tagarticle['seo_des'] : $Config["web_description"];
unset($tagproduct);
}
if (!empty($tid)) { // 欄目頁(yè)面
$tagarctype = Arctype::find($tid);
$t = !empty($tagarctype['seo_title']) ? $tagarctype['seo_title'] : $tagarctype["title"]."-".$Config["web_name"];
$k = !empty($tagarctype['seo_key']) ? $tagarctype['seo_key'] : $Config["web_keywords"];
$d = !empty($tagarctype['seo_des']) ? $tagarctype['seo_des'] : $Config["web_description"];
unset($tagarctype);
}
if (!empty($pid)) { // 產(chǎn)品頁(yè)面
$tagproduct = Goods::find($pid);
$t = !empty($tagproduct['seo_title']) ? $tagproduct['seo_title'] : $tagproduct["title"]."-".$Config["web_name"];
$k = !empty($tagproduct['seo_key']) ? $tagproduct['seo_key'] : $Config["web_keywords"];
$d = !empty($tagproduct['seo_des']) ? $tagproduct['seo_des'] : $Config["web_description"];
unset($tagproduct);
}
return sprintf($title, htmlentities($t),htmlentities($d),htmlentities($k));
}然后就可以在模板中調(diào)用標(biāo)簽了
{ht:title}會(huì)自動(dòng)生成title標(biāo)題,Description描述,Keywords關(guān)鍵詞
