For AI agents: the complete documentation index is available at https://docs.halo.run/llms.txt, the full documentation bundle is available at https://docs.halo.run/llms-full.txt, and this page is available as Markdown at https://docs.halo.run/developer-guide/theme/template-variables/category.md.

分类归档

用于根据分类列出所有文章的页面。

路由信息

  • 模板路径:/templates/category.html
  • 访问路径:/categories/:slug

自定义模板

除了上面提到的 category.html,主题作者还可以添加多种形式的额外渲染模板,提供给用户选择,可以通过这个功能实现将网站上的文章内容进行领域划分,比如网站上同时存在新闻、文档、博客等分区,那么就可以利用这个功能提供多个模板,同时 Halo 还支持为分类设置文章渲染模板,详情可见新建文章分类

定义方式为:

theme.yaml
customTemplates:
  category:
    - name: {name}
      description: {description}
      screenshot: {screenshot}
      file: {file}.html
  • name:模板名称
  • description:模板描述
  • screenshot:模板预览图
  • file:模板文件名,需要在 /templates/ 目录下创建

示例:

theme.yaml
customTemplates:
  category:
    - name: 新闻
      description: 用于展示新闻分类下的文章
      screenshot:
      file: category_news.html
    - name: 博客
      description: 用于展示博客分类下的文章
      screenshot:
      file: category_blog.html
修改 theme.yaml 后重载配置

需要注意,修改 theme.yaml 需要重载主题配置

变量

category

变量类型

#CategoryVo

posts

变量类型

#UrlContextListResult<ListedPostVo>

示例

/templates/category.html
<div>
  <h1 th:text="${category.spec.displayName}"></h1>
  <ul>
    <li th:each="post : ${posts.items}">
      <a th:text="${post.spec.title}" th:href="${post.status.permalink}"></a>
    </li>
  </ul>
  <div th:if="${posts.hasPrevious() || posts.hasNext()}">
    <a th:href="@{${posts.prevUrl}}">
      <span>上一页</span>
    </a>
    <span th:text="${posts.page} +' / '+ ${posts.total}"></span>
    <a th:href="@{${posts.nextUrl}}">
      <span>下一页</span>
    </a>
  </div>
</div>

_templateId

变量值

category

类型定义

CategoryVo

CategoryVo
{
  "metadata": {
    "name": "string", // 唯一标识
    "labels": {
      "additionalProp1": "string",
    },
    "annotations": {
      "additionalProp1": "string",
    },
    "creationTimestamp": "2022-11-20T13:06:38.512Z", // 创建时间
  },
  "spec": {
    "displayName": "string", // 显示名称
    "slug": "string", // 别名,通常用于生成 status.permalink
    "description": "string", // 描述
    "cover": "string", // 封面图
    "template": "string", // 自定义渲染模板名称
    "priority": 0, // 排序字段
    "children": [
      // 下级分类,分类的 metadata.name 集合
      "string",
    ],
  },
  "status": {
    "permalink": "string", // 固定链接
  },
  "postCount": 0, // 文章数量
}

TagVo

TagVo
{
  "metadata": {
    "name": "string", // 唯一标识
    "labels": {
      "additionalProp1": "string",
    },
    "annotations": {
      "additionalProp1": "string",
    },
    "creationTimestamp": "2022-11-20T13:06:38.512Z", // 创建时间
  },
  "spec": {
    "displayName": "string", // 显示名称
    "slug": "string", // 别名,通常用于生成 status.permalink
    "color": "#F9fEB1", // 背景颜色
    "cover": "string", // 封面图
  },
  "status": {
    "permalink": "string", // 固定链接
  },
  "postCount": 0, // 文章数量
}

ContributorVo

ContributorVo
{
  "name": "string", // 用户名
  "displayName": "string", // 显示名称
  "avatar": "string", // 头像
  "bio": "string", // 描述
  "permalink": "string", // 作者的文章归档页面链接
  "metadata": {
    "name": "string", // 唯一标识
    "labels": {
      "additionalProp1": "string",
    },
    "annotations": {
      "additionalProp1": "string",
    },
    "creationTimestamp": "2022-11-20T13:06:38.512Z", // 创建时间
  },
}

ListedPostVo

ListedPostVo
{
  "metadata": {
    "name": "string", // 唯一标识
    "labels": {
      "additionalProp1": "string",
    },
    "annotations": {
      "additionalProp1": "string",
    },
    "creationTimestamp": "2022-11-20T13:06:38.505Z", // 创建时间
  },
  "spec": {
    "title": "string", // 标题
    "slug": "string", // 别名,通常用于生成 status.permalink
    "releaseSnapshot": "string",
    "headSnapshot": "string",
    "baseSnapshot": "string",
    "owner": "string", // 创建者名称,即 ContributorVo 的 metadata.name,非显示名称
    "template": "string", // 自定义渲染模板
    "cover": "string", // 封面图
    "deleted": false,
    "publish": false,
    "publishTime": "2022-11-20T13:06:38.505Z", // 发布时间
    "pinned": false, // 是否置顶
    "allowComment": true, // 是否允许评论
    "visible": "PUBLIC",
    "priority": 0,
    "excerpt": {
      "autoGenerate": true, // 是否自动生成摘要
      "raw": "string", // 摘要内容
    },
    "categories": [
      // 分类的名称集合,即 Category 的 metadata.name 的集合
      "string",
    ],
    "tags": [
      // 标签的名称集合,即 Tag 的 metadata.name 的集合
      "string",
    ],
    "htmlMetas": [
      {
        "additionalProp1": "string",
      },
    ],
  },
  "status": {
    "permalink": "string", // 固定链接
    "excerpt": "string", // 最终生成的摘要
    "inProgress": true,
    "lastModifyTime": "2022-11-20T13:06:38.505Z", // 最后修改时间
    "commentsCount": 0, // 评论数
    "contributors": [
      // 贡献者名称,Contributor 的 metadata.name 的集合
      "string",
    ],
  },
  "categories": "List<#CategoryVo>", // 分类的集合
  "tags": "List<#TagVo>", // 标签的集合
  "contributors": "List<#ContributorVo>", // 贡献者的集合
  "owner": "#ContributorVo", // 创建者
  "stats": {
    "visit": 0, // 访问数量
    "upvote": 0, // 点赞数量
    "comment": 0, // 评论数量
  },
}

UrlContextListResult<ListedPostVo>

UrlContextListResult<ListedPostVo>
{
  "page": 0, // 当前页码
  "size": 0, // 每页条数
  "total": 0, // 总条数
  "items": "List<#ListedPostVo>", // 文章列表数据
  "first": true, // 是否为第一页
  "last": true, // 是否为最后一页
  "hasNext": true, // 是否有下一页
  "hasPrevious": true, // 是否有上一页
  "totalPages": 0, // 总页数
  "nextUrl": "string", // 下一页链接
  "prevUrl": "string", // 上一页链接
}