🎨 主题与外观
TBlog 内置两套精心设计的主题,支持全站默认和文章级独立切换。
📦 内置主题
🌿
Flow
清新明亮的浅色主题,适合技术文章与生活分享,大量留白,长文阅读舒适。
🌙
Magine
沉浸深邃的深色主题,适合夜间阅读与创作,配色护眼,代码块高亮更突出。
🌐 切换全站默认主题
- 登录管理后台:
http://你的域名/admin/ - 进入「设置」页面
- 在「默认主题」下拉框选择
flow或magine - 点击「保存设置」
💡
提示
全站默认主题只对新文章生效,已发布的文章仍然使用各自设置的主题。
📄 单独设置某篇文章的主题
- 进入「文章管理」
- 编辑你要修改的文章
- 在右侧「模板」下拉框选择
flow/magine/default(继承全站设置) - 保存
📁 主题目录结构
主题文件都在 templates/ 目录下:
templates/ ├── flow/ # Flow 主题 │ ├── index.php # 首页/列表 │ ├── post.php # 文章详情 │ ├── style.css # 样式 │ └── header.php # 页头(可选) └── magine/ # Magine 主题 ├── index.php ├── post.php └── style.css
🛠 自定义主题
基于现有主题修改最简单。复制一份然后改:
# 复制 Flow 主题作为新主题 cp -r templates/flow templates/mytheme
然后编辑 templates/mytheme/style.css,修改配色、字体、间距等。后台设置里就能选到 mytheme 了。
可用的主题变量
| 变量 | 说明 | 示例值 |
|---|---|---|
$site_name | 站点名称 | 我的博客 |
$site_desc | 站点描述 | 简洁优雅 |
$post_title | 文章标题 | 第一篇文章 |
$post_content | 文章内容(已净化) | HTML |
$post_date | 发布日期 | 2024-05-31 |
$post_category | 分类 | 技术 |
$post_tags | 标签数组 | ['PHP', '博客'] |
$comments | 评论列表 | array |
🆕 从零开发新主题
创建 templates/your-theme/ 目录,最少需要两个文件:
index.php(首页 / 列表页)
templates/your-theme/index.php
<!DOCTYPE html> <html> <head> <title><?= htmlspecialchars($site_name) ?></title> <link rel="stylesheet" href="style.css"> </head> <body> <h1><?= htmlspecialchars($site_name) ?></h1> <p><?= htmlspecialchars($site_desc) ?></p> <ul> <?php foreach ($posts as $post): ?> <li> <a href="?p=<?= $post['id'] ?>"> <?= htmlspecialchars($post['title']) ?> </a> <small><?= $post['created_at'] ?></small> </li> <?php endforeach; ?> </ul> </body> </html>
post.php(文章详情)
templates/your-theme/post.php
<article> <h1><?= htmlspecialchars($post_title) ?></h1> <time><?= $post_date ?></time> <div class="content"> <?= $post_content ?> <!-- 已 HTMLPurifier 净化 --> </div> </article>
🎨
做出好主题了?
欢迎提交 PR 到 GitHub 仓库,让更多人用上你的设计。