主题
Snow 通过主题系统管理站点外观。主题包含模板、静态资源和配置文件。
使用主题
1theme: "mytheme"
| 配置项 | 默认值 | 说明 |
|---|---|---|
theme |
— | 主题名称,对应 themes/{name}/ 目录 |
主题目录结构
1themes/mytheme/ 2├── theme.yaml # 主题配置(可选) 3├── templates/ # 模板(固定名) 4│ ├── index.html # 首页 5│ ├── page.html # 单页 6│ ├── section.html # 栏目页 7│ ├── taxonomy_list.html # 分类列表 8│ ├── taxonomy_single.html # 分类详情 9│ └── shortcodes/ # 短代码模板 10│ └── youtube.html 11├── static/ # 静态资源(固定名) 12│ ├── css/ 13│ ├── js/ 14│ └── images/ 15└── i18n/ # 翻译文件 16 ├── en.yaml 17 └── zh.yaml
themes、主题内的 templates、static、i18n 目录名不可修改。
主题配置 (theme.yaml)
主题根目录下的 theme.yaml(或 .toml、.json)会在加载时自动合并到站点配置:
1# themes/mytheme/theme.yaml 2name: "MyTheme" 3version: "1.0.0" 4description: "A minimal theme" 5author: "honmaple" 6 7# 以下会合并到站点配置,但不覆盖站点已设置的项 8params: 9 mytheme_primary_color: "#333"
合并规则:站点已有配置项不会被主题配置覆盖。
模板查找优先级
templates/ 目录的合并顺序(先匹配的文件优先):
- 站点
templates/目录 - 主题
templates/目录 - 内置默认主题
templates/目录
因此只需在站点 templates/ 中创建与主题同名的文件即可覆盖:
1mysite/ 2├── templates/ 3│ └── page.html # 覆盖主题的 page.html 4└── themes/ 5 └── mytheme/ 6 └── templates/ 7 ├── index.html 8 └── page.html # 被覆盖
静态资源优先级
- 站点
static/目录 - 主题
static/目录
同名文件以站点为准。
创建最小主题
themes/simple/:
1simple/ 2├── theme.yaml 3├── templates/ 4│ ├── index.html 5│ └── page.html 6└── static/ 7 └── css/ 8 └── style.css
index.html
1<!DOCTYPE html> 2<html lang="{{ config.language }}"> 3<head> 4 <meta charset="utf-8"> 5 <title>{{ config.title }}</title> 6 <link rel="stylesheet" href="{{ config.base_url }}/css/style.css"> 7</head> 8<body> 9 <h1>{{ config.title }}</h1> 10 {% for section in sections %} 11 <section> 12 <h2><a href="{{ section.Path }}">{{ section.Title }}</a></h2> 13 <ul> 14 {% for page in section.Pages %} 15 <li><a href="{{ page.Path }}">{{ page.Title }}</a></li> 16 {% endfor %} 17 </ul> 18 </section> 19 {% endfor %} 20</body> 21</html>
page.html
1<!DOCTYPE html> 2<html lang="{{ page.Lang }}"> 3<head> 4 <meta charset="utf-8"> 5 <title>{{ page.Title }} - {{ config.title }}</title> 6 <link rel="stylesheet" href="{{ config.base_url }}/css/style.css"> 7</head> 8<body> 9 <article> 10 <h1>{{ page.Title }}</h1> 11 <time>{{ page.Date | date:"2006-01-02" }}</time> 12 <div>{{ page.Content | safe }}</div> 13 </article> 14</body> 15</html>