页面数据

添加自定义数据到页面

页面数据

页面可以包含任意数据。在 Markdown 文件中,数据定义在 front matter 区块中,这是一个由两组三短划线分隔的区块,其中包含 YAML 代码。还有其他格式可以拥有 front matter 或者以不同的方式存储数据。让我们看一些例子:

---
title: This is the title
url: custom-url.html
---

This is the page content
title: This is the title
url: custom-url.html
content: This is the page content
---
title: This is the title
url: custom-url.html
---

<h1>{{ title }}</h1>
This is the page content
{
  "title": "This is the title",
  "url": "custom-url.html",
  "content": "This is the page content"
}
export const title = "This is the title";
export const url = "custom-url.html";

export default () => "<p>This is the page content</p>";

在上面的例子中,所有页面都包含两个变量:titleurl

在使用 front matter 的格式(如 Markdown、Vento 和 Nunjucks)中,内容定义在 front matter 下方。不使用 front matter 的格式将内容导出为 content 变量,或者可选地,作为默认导出(如 page.page.js 中所示)。

标准变量

有一些特殊的变量 Lume 可以识别:

url

url 变量包含页面的公共 URL,用于创建链接和配置输出文件名。如果它不存在,则由 Lume 自动生成。请参阅 URL docs

date

如果未定义,Lume 会自动使用文件创建日期。此变量可以在文件名 页面日期 或 front matter 中定义。接受的值包括:

  • 任何 IS0 8601 兼容的日期,例如 2021-01-012021-01-01 03:10:102021-01-01T03:10:10Z2021-01-01Y03:10:10-0700 等。
  • 特殊值 Git Created 用于获取此文件首次添加到 Git 历史记录中的时间。它使用创建日期作为回退。
  • 特殊值 Git Last Modified 用于获取此文件在 Git 历史记录中最后一次更改的时间。它使用最后修改日期作为回退。

draft

draft 变量将此页面标记为草稿,这意味着除非环境变量 LUME_DRAFTS 定义为 "true",否则它将被忽略。

layout

用于定义用于渲染页面的布局。有关更多信息,请参阅 布局

tags

tags 用于对页面进行分组。请参阅 标签

templateEngine

用于覆盖用于渲染页面的模板引擎。请参阅 模板引擎

renderOrder

用于自定义页面渲染的顺序。请参阅 渲染顺序

mergedKeys

用于自定义某些数据的合并方式。请参阅 合并键

---
url: /welcome.html
date: 2021-01-01
layout: layouts/post.vto
draft: true
tags: post
---
export const url = "/welcome.html";
export const date = new Date("2021-01-01T03:24:00");
export const layout = "layouts/post.vto";
export const draft = true;
export const tags = ["post"];
const data = {
  url: "/welcome.html",
  date: new Date("2021-01-01T03:24:00"),
  layout: "layouts/post.vto",
  draft: true,
  tags: ["post"],
};

export default data;