页面布局

本页内容

让编辑者从编辑器的下拉菜单中为每个页面选择布局(例如默认、全宽、着陆页),使用映射到页面路由中布局组件的 select 字段。

工作原理

  1. 向页面集合添加 template 选择字段
  2. 为每个选项创建布局组件
  3. 在页面路由中将字段值映射到布局

这使用了 EmDash 的选择字段和 Astro 的组件模型。

添加字段

在管理界面中,向页面集合添加一个带有 slug template 和布局选项(例如 “Default”、“Full Width”)的选择字段。或者将其包含在种子数据中:

{
  "slug": "template",
  "label": "Template",
  "type": "select",
  "validation": {
    "options": ["Default", "Full Width"]
  },
  "defaultValue": "Default"
}

创建布局组件

每个布局以不同的样式包装基础布局中的内容:

---
import type { ContentEntry } from "emdash";
import { PortableText } from "emdash/ui";
import Base from "./Base.astro";

interface Props {
  page: ContentEntry<any>;
}

const { page } = Astro.props;
---

<Base title={page.data.title}>
  <article class="page-default">
    <h1>{page.data.title}</h1>
    <PortableText value={page.data.content} />
  </article>
</Base>

<style>
  .page-default {
    max-width: var(--content-width);
    margin: 0 auto;
    padding: 2rem 1rem;
  }
</style>
---
import type { ContentEntry } from "emdash";
import { PortableText } from "emdash/ui";
import Base from "./Base.astro";

interface Props {
  page: ContentEntry<any>;
}

const { page } = Astro.props;
---

<Base title={page.data.title}>
  <article class="page-wide">
    <h1>{page.data.title}</h1>
    <PortableText value={page.data.content} />
  </article>
</Base>

<style>
  .page-wide {
    max-width: var(--wide-width);
    margin: 0 auto;
    padding: 2rem 1rem;
  }
</style>

连接路由

在页面路由中,导入每个布局并映射模板值:

---
import { getEmDashEntry } from "emdash";
import PageDefault from "../../layouts/PageDefault.astro";
import PageFullWidth from "../../layouts/PageFullWidth.astro";

const { slug } = Astro.params;

if (!slug) {
  return Astro.redirect("/404");
}

const { entry: page } = await getEmDashEntry("pages", slug);

if (!page) {
  return Astro.redirect("/404");
}

const layouts = {
  "Default": PageDefault,
  "Full Width": PageFullWidth,
};

const Layout = layouts[page.data.template as keyof typeof layouts] ?? PageDefault;
---

<Layout page={page} />

路由保持精简。每个布局组件拥有自己的标记和样式。添加布局就是:创建组件,向选择字段添加选项,向映射添加一行。

添加更多布局

常见的布局选择:

  • Default — 窄内容列,适合阅读
  • Full Width — 更宽的内容区域,无侧边栏
  • Landing Page — 无页眉/页脚,英雄区域
  • Sidebar — 带有侧边栏小部件区域的内容

每个都只是 src/layouts/ 目录中的另一个 Astro 组件,以及路由布局映射中的另一个条目。