title | description | type | service | stub | i18nReady |
---|---|---|---|---|---|
ButterCMS & Astro |
使用 ButterCMS 向你的 Astro 项目添加内容 |
cms |
ButterCMS |
false |
true |
import { Steps } from '@astrojs/starlight/components'; import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'
ButterCMS 是一种允许你在项目中发布结构化内容的 headless CMS和博客引擎。
:::note 要查看完整的博客网站示例,请参阅 Astro + ButterCMS 入门项目。 ::: 在本节中,我们将使用 ButterCMS SDK 将数据引入你的 Astro 项目中。 要开始使用,你将需要以下内容:
-
Astro 项目 -如果你还没有 Astro 项目,我们的安装指南将帮助你快速创建并运行一个项目。
-
ButterCMS 账号. 如果你还没有 ButterCMS 账号,你可以通过注册 免费试用来创建一个。
-
你的 ButterCMS API TOKEN - 你可以在设置 页面找到你的API TOKEN。
```ini title=".env"
BUTTER_TOKEN=YOUR_API_TOKEN_HERE
```
:::tip
了解更多关于[使用环境变量](/zh-cn/guides/environment-variables/)和Astro中的 .env 文件的信息。
:::
-
将 ButterCMS SDK 安装为依赖项:
```shell pnpm add buttercms ``` ```shell yarn add buttercms ```npm install buttercms
-
在你的项目中创建一个新的
src/lib/
目录,并在其中创建一个名为buttercms.js
的文件import Butter from "buttercms"; export const butterClient = Butter(import.meta.env.BUTTER_TOKEN);
这将使用你的 API TOKEN 对 SDK 进行身份验证,并将其导出供整个项目使用。
要获取内容,导入这个客户端并使用其中的一个 retrieve
函数。
在这个例子中,我们将会检索一个集合,
该集合有三个字段:一个短文本字段 name
,一个数字字段 price
和一个 WYSIWYG 的富文本字段 description
。
---
import { butterClient } from "../lib/buttercms";
const response = await butterClient.content.retrieve(["shopitem"]);
interface ShopItem {
name: string,
price: number,
description: string,
}
const items = response.data.data.shopitem as ShopItem[];
---
<body>
{items.map(item => <div>
<h2>{item.name} - ${item.price}</h2>
<p set:html={item.description}></p>
</div>)}
</body>
这个接口反映了字段的类型。WYSIWYG 富文本字段 description
加载为 HTML 字符串,而 set:html
指令将 HTML 字符串注入元素中进行渲染。
类似地,你可以获取单个页面并显示它的字段:
---
import { butterClient } from "../lib/buttercms";
const response = await butterClient.page.retrieve("*", "simple-page");
const pageData = response.data.data;
interface Fields {
seo_title: string,
headline: string,
hero_image: string,
}
const fields = pageData.fields as Fields;
---
<html>
<title>{fields.seo_title}</title>
<body>
<h1>{fields.headline}</h1>
<img src={fields.hero_image} />
</body>
</html>
- Astro + ButterCMS 入门项目
- ButterCMS API 文档
- ButterCMS 的 JavaScript 指南
- 欢迎添加你的资源!