输出格式 (Format)
Snow 可为 Section、Page、Taxonomy Term 生成 RSS、Atom、JSON 等多种输出格式。
全局默认值
1formats: 2 rss: 3 template: "partials/rss.xml" 4 atom: 5 template: "partials/atom.xml"
定义全局默认模板,各内容类型可覆盖路径。
Section 格式
通过 Section FrontMatter 配置:
1# _index.md 2--- 3formats: 4 rss: 5 path: "posts/index.xml" 6 atom: 7 path: "posts/atom.xml" 8 json: 9 path: "posts/index.json" 10 template: "custom.json" 11---
path: ""禁用该格式- 未设
template时使用全局默认模板
Page 格式
1--- 2formats: 3 json: 4 path: "api/articles/hello.json" 5 template: "article.json" 6---
Taxonomy Term 格式
1taxonomies: 2 tags: 3 term: 4 formats: 5 atom: 6 path: "tags/{term:slug}/atom.xml" 7 template: "custom.atom.xml"
格式查找逻辑
- FrontMatter 中的
formats.{name}map formats.{name}.path— 输出路径(必须)formats.{name}.template— 模板(可选,为空则从全局formats.{name}.template取值)path为空或template为空时跳过该格式
模板变量
所有格式模板都会继承全局模板变量和函数,例如 pages、sections、taxonomies、get_pages([lang])。当前内容对象按格式类型额外注入:
| 内容类型 | 当前对象变量 | 当前页面集合 |
|---|---|---|
| Section | section |
section.Pages |
| Page | page |
无 |
| Taxonomy Term | term, taxonomy |
term.Pages |
pages 始终表示当前语言的全局页面列表,不再作为 Section 或 Taxonomy Term 格式模板里的局部页面集合别名。
RSS 模板示例
templates/partials/rss.xml:
1<?xml version="1.0" encoding="utf-8"?> 2<rss version="2.0"> 3 <channel> 4 <title>{{ config.title }}</title> 5 <link>{{ config.base_url }}</link> 6 <description>{{ config.description }}</description> 7 {% for page in section.Pages %} 8 <item> 9 <title>{{ page.Title }}</title> 10 <link>{{ page.Permalink }}</link> 11 <pubDate>{{ page.Date | date:"Mon, 02 Jan 2006 15:04:05 -0700" }}</pubDate> 12 <description>{{ page.Summary }}</description> 13 </item> 14 {% endfor %} 15 </channel> 16</rss>
Atom 模板示例
1<?xml version="1.0" encoding="utf-8"?> 2<feed xmlns="http://www.w3.org/2005/Atom"> 3 <title>{{ config.title }}</title> 4 <link href="{{ config.base_url }}/atom.xml" rel="self"/> 5 <updated>{{ section.Pages[0].Date | date:"2006-01-02T15:04:05Z07:00" }}</updated> 6 <id>{{ config.base_url }}/</id> 7 {% for page in section.Pages %} 8 <entry> 9 <title>{{ page.Title }}</title> 10 <link href="{{ page.Permalink }}"/> 11 <id>{{ page.Permalink }}</id> 12 <published>{{ page.Date | date:"2006-01-02T15:04:05Z07:00" }}</published> 13 <content type="html">{{ page.Content }}</content> 14 </entry> 15 {% endfor %} 16</feed>