帝國 CMS(EmpireCMS)作為國內老牌企業級 CMS,其模板系統支援「靈動標簽」在列表頁、首頁靈活輸出內容。常見需求:根據文章屬性(有圖、置頂、推薦、頭條)在標題前顯示對應圖標。本文系統整理靈動標簽與列表模板兩種場景的實現代碼,並提供組合判斷邏輯,可直接複製應用。
| 字段 | 含義 | 值說明 |
|---|---|---|
titlepic | 標題圖片 | 非空表示有縮略圖 |
isgood | 推薦 | 1=推薦,0=不推薦 |
firsttitle | 頭條 | 1=頭條,0=非頭條 |
istop | 置頂 | 1=置頂,0=不置頂 |
truetime | 真實發布時間 | Unix 時間戳 |
$bqr:字段值陣列,如 $bqr['title']、$bqr['isgood']$bqsr:系統回傳值,如 $bqsr['titleurl'](標題鏈接)[e:loop={'selfinfo',20,0,0}]
<?php
$newimg = '';
$good = '';
// 有標題圖顯示圖片圖標
if (!empty($bqr['titlepic'])) {
$newimg = "<img src='https://www.example.com/uploads/allimg/140411/22561T200-0.gif' alt='圖文'>";
}
// 置頂優先級最高
if ($bqr['istop'] == 1) {
$good = "<img src='https://www.example.com/uploads/allimg/140411/22561UD6-1.gif' alt='置頂'>";
} elseif ($bqr['firsttitle'] == 1) {
$good = "<img src='https://www.example.com/uploads/allimg/140411/22561UD6-2.gif' alt='頭條'>";
} elseif ($bqr['isgood'] == 1) {
$good = "<img src='https://www.example.com/uploads/allimg/140411/22561UD6-3.gif' alt='推薦'>";
}
// 3 天內顯示 NEW
if (time() - $bqr['truetime'] <= 3 * 24 * 3600) {
$newimg .= "<img src='https://www.example.com/uploads/allimg/140411/22561T200-1.gif' alt='NEW'>";
}
?>
<tr class="even">
<td class="title">
<h1>
<a href="<?=$bqsr['titleurl']?>" target="_blank"><?=esub($bqr['title'], 30, false)?></a>
<?=$newimg?> <?=$good?>
</h1>
<p class="intro"><?=esub($bqr['smalltext'], 80, false)?>[<a href="<?=$bqsr['titleurl']?>" target="_blank">詳細內容</a>]</p>
</td>
<td><?=$bqr['myarea']?></td>
<td><?=date('Y-m-d H:i:s', $bqr['newstime'])?></td>
</tr>
[/e:loop]
$r:字段值陣列,如 $r['title']、$r['isgood']$listtemp<?php
$newimg = '';
if ($r['isgood'] == 1) {
$newimg = "<img src='https://www.example.com/icon/good.gif' alt='推薦'>";
} elseif ($r['firsttitle'] == 1) {
$newimg = "<img src='https://www.example.com/icon/first.gif' alt='頭條'>";
} elseif ($r['istop'] == 1) {
$newimg = "<img src='https://www.example.com/icon/top.gif' alt='置頂'>";
}
$listtemp = '<li><a href="[!--titleurl--]" title="[!--oldtitle--]">[!--title--]</a>' . $newimg . '</li>';
?>
<?php // 單一條件 if ($bqr['isgood'] == 1) // 推薦 if ($bqr['firsttitle'] == 1) // 頭條 if ($bqr['istop'] == 1) // 置頂 // 組合條件(同時滿足) if ($bqr['isgood'] && $bqr['firsttitle'] && $bqr['istop']) // 置頂+頭條+推薦 // 優先級判斷:置頂 > 頭條 > 推薦 $badge = ''; if ($bqr['istop']) $badge = 'top'; elseif ($bqr['firsttitle']) $badge = 'first'; elseif ($bqr['isgood']) $badge = 'good'; ?>
<?php
// 定義圖標映射,便於維護
$iconMap = [
'istop' => ['url' => '/static/icon/top.svg', 'alt' => '置頂', 'class' => 'badge-top'],
'firsttitle' => ['url' => '/static/icon/first.svg', 'alt' => '頭條', 'class' => 'badge-first'],
'isgood' => ['url' => '/static/icon/good.svg', 'alt' => '推薦', 'class' => 'badge-good'],
'haspic' => ['url' => '/static/icon/image.svg', 'alt' => '圖文', 'class' => 'badge-img'],
'new' => ['url' => '/static/icon/new.svg', 'alt' => '最新', 'class' => 'badge-new'],
];
$badges = '';
foreach (['istop', 'firsttitle', 'isgood'] as $key) {
if (!empty($bqr[$key])) {
$ic = $iconMap[$key];
$badges .= "<img src='{$ic['url']}' alt='{$ic['alt']}' class='{$ic['class']}'>";
break; // 只顯示最高優先級
}
}
if (!empty($bqr['titlepic'])) {
$ic = $iconMap['haspic'];
$badges .= "<img src='{$ic['url']}' alt='{$ic['alt']}' class='{$ic['class']}'>";
}
if (time() - $bqr['truetime'] <= 3 * 86400) {
$ic = $iconMap['new'];
$badges .= "<img src='{$ic['url']}' alt='{$ic['alt']}' class='{$ic['class']}'>";
}
?>
<h2><a href="<?=$bqsr['titleurl']?>"><?=esub($bqr['title'], 36)?></a> <?=$badges?></h2>
truetime 與 newstime 可能不同,前者為真實發布時間,後者可能為排序時間$r,不能用 $bqr;且必須賦值給 $listtemp帝國 CMS 靈動標簽提供了極大的模板靈活性,掌握 $bqr/$r 字段陣列與條件判斷邏輯,即可實現各種「標簽圖標」業務需求。建議將圖標路徑、優先級邏輯封裝為公共片段(或自定義函數),統一維護,避免模板散落重複代碼。