Astro はコンテンツ中心のサイト向け Web フレームワークです。EmDash と組み合わせると、Astro が WordPress テーマの代わりになり—テンプレート、ルーティング、レンダリングを担います。
このガイドは、すでに理解している WordPress の概念に対応づけながら Astro の基本を説明します。
重要なパラダイムシフト
デフォルトはサーバーレンダリング
PHP と同様、Astro のコードはサーバーで動きます。PHP とは違い、デフォルトでは JavaScript ゼロの静的 HTML を出力します。
足さない限り JS はゼロ
WordPress は jQuery やテーマのスクリプトを自動で読み込みます。Astro は明示的に足さない限りブラウザに何も送りません。
コンポーネント指向のアーキテクチャ
散らばったテンプレートタグや include の代わりに、合成可能で自己完結したコンポーネントで組み立てます。
ファイルベースのルーティング
リライトルールや query_vars は不要。src/pages/ のファイル構造が URL を直接決めます。
プロジェクト構成
WordPress テーマは「魔法のファイル名」を持つフラットな構造になりがちです。Astro は明示的なディレクトリを使います。
| WordPress | Astro | 役割 |
|---|---|---|
index.php, single.php | src/pages/ | ルート(URL) |
template-parts/ | src/components/ | 再利用可能な UI 部品 |
header.php + footer.php | src/layouts/ | ページのラッパー |
style.css | src/styles/ | グローバル CSS |
functions.php | astro.config.mjs | サイト設定 |
典型的な Astro プロジェクト:
src/
├── components/ # Reusable UI (Header, PostCard, etc.)
├── layouts/ # Page shells (Base.astro)
├── pages/ # Routes - files become URLs
│ ├── index.astro # → /
│ ├── posts/
│ │ ├── index.astro # → /posts
│ │ └── [slug].astro # → /posts/hello-world
│ └── [slug].astro # → /about, /contact, etc.
└── styles/
└── global.css
Astro コンポーネント
.astro ファイルは PHP テンプレートに相当します。各ファイルは 2 部構成です。
- フロントマター(
---で囲む部分)— テンプレート先頭の PHP のようなサーバー側コード - テンプレート— PHP テンプレートの残りと同様、式付き HTML
---
// Frontmatter: runs on server, never sent to browser
interface Props {
title: string;
excerpt: string;
url: string;
}
const { title, excerpt, url } = Astro.props;
---
<!-- Template: outputs HTML -->
<article class="post-card">
<h2><a href={url}>{title}</a></h2>
<p>{excerpt}</p>
</article>
PHP との主な違い:
- フロントマターは隔離されている。 そこで宣言した変数はテンプレートで使えるが、コード自体はブラウザに届かない。
- import はフロントマターに書く。 コンポーネント、データ、ユーティリティ—すべて上部で import。
- TypeScript が使える。
interface Propsで prop 型を定義し、エディタの補完と検証に使う。
テンプレート式
Astro のテンプレートは <?php ?> の代わりに {波括弧} を使います。構文は JSX に似ていますが、出力は純粋な HTML です。
Astro
---
import { getEmDashCollection } from "emdash";
const { entries: posts } = await getEmDashCollection("posts");
const showTitle = true;
---
{showTitle && <h1>Latest Posts</h1>}
{posts.length > 0 ? (
<ul>
{posts.map(post => (
<li>
<a href={`/posts/${post.id}`}>{post.data.title}</a>
</li>
))}
</ul>
) : (
<p>No posts found.</p>
)} PHP
<?php
$posts = new WP_Query(['post_type' => 'post']);
$show_title = true;
?>
<?php if ($show_title): ?>
<h1>Latest Posts</h1>
<?php endif; ?>
<?php if ($posts->have_posts()): ?>
<ul>
<?php while ($posts->have_posts()): $posts->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
<?php else: ?>
<p>No posts found.</p>
<?php endif; ?> 式のパターン
| Pattern | 目的 |
|---|---|
{variable} | 値を出力 |
{condition && <Element />} | 条件付きレンダリング |
{condition ? <A /> : <B />} | If/else |
{items.map(item => <Li>{item}</Li>)} | ループ |
Props とスロット
コンポーネントは props(関数引数のようなもの)と slots(do_action の挿入点のようなもの)でデータを受け取ります。
Astro
---
interface Props {
title: string;
featured?: boolean;
}
const { title, featured = false } = Astro.props;
---
<article class:list={["card", { featured }]}>
<h2>{title}</h2>
<slot />
<slot name="footer" />
</article>使用例:
<Card title="Hello" featured>
<p>This goes in the default slot.</p>
<footer slot="footer">Footer content</footer>
</Card> PHP
<?php
// Usage: get_template_part('template-parts/card', null, [
// 'title' => 'Hello',
// 'featured' => true
// ]);
$title = $args['title'] ?? '';
$featured = $args['featured'] ?? false;
$class = $featured ? 'card featured' : 'card';
?>
<article class="<?php echo esc_attr($class); ?>">
<h2><?php echo esc_html($title); ?></h2>
<?php
// No direct equivalent to slots.
// WordPress uses do_action() for similar patterns:
do_action('card_content');
do_action('card_footer');
?>
</article> Props と $args
WordPress では get_template_part() が $args 配列でデータを渡します。Astro の props は型付きで分割代入します。
---
// Type-safe with defaults
interface Props {
title: string;
count?: number;
}
const { title, count = 10 } = Astro.props;
---
スロットとフック
WordPress は do_action() で挿入点を作ります。Astro はスロットを使います。
| WordPress | Astro |
|---|---|
do_action('before_content') | <slot name="before" /> |
| Default content area | <slot /> |
do_action('after_content') | <slot name="after" /> |
違い: スロットは呼び出し側で子要素を受け取り、WordPress のフックは別ファイルの add_action() が必要です。
レイアウト
レイアウトはページを共通 HTML 構造—<head>、ヘッダー、フッター、共有要素—で包みます。header.php + footer.php に相当します。
---
import "../styles/global.css";
interface Props {
title: string;
description?: string;
}
const { title, description = "My EmDash Site" } = Astro.props;
---
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content={description} />
<title>{title}</title>
</head>
<body>
<header>
<nav><!-- Navigation --></nav>
</header>
<main>
<slot />
</main>
<footer>
<p>© {new Date().getFullYear()}</p>
</footer>
</body>
</html>
ページでレイアウトを使う:
---
import Base from "../layouts/Base.astro";
---
<Base title="Home">
<h1>Welcome</h1>
<p>Page content goes in the slot.</p>
</Base>
スタイリング
Astro には複数のスタイル手法があります。特徴的なのは スコープ付きスタイル です。
スコープ付きスタイル
<style> 内のスタイルは自動的にそのコンポーネントに限定されます。
<article class="card">
<h2>Title</h2>
</article>
<style>
/* Only affects .card in THIS component */
.card {
padding: 1rem;
border: 1px solid #ddd;
}
h2 {
color: navy;
}
</style>
生成 HTML には一意のクラス名が付き、スタイルの漏れを防ぎます。詳細度の争いに悩みにくくなります。
グローバルスタイル
サイト全体用の CSS ファイルを作り、レイアウトで import します。
---
import "../styles/global.css";
---
条件付きクラス
class:list ディレクティブはクラス文字列の手組み立てに代わります。
Astro
---
const { featured, size = "medium" } = Astro.props;
---
<article class:list={[
"card",
size,
{ featured, "has-border": true }
]}>出力: <article class="card medium featured has-border">
PHP
<?php
$classes = ['card', $size];
if ($featured) $classes[] = 'featured';
if (true) $classes[] = 'has-border';
?>
<article class="<?php echo esc_attr(implode(' ', $classes)); ?>"> クライアント側 JavaScript
Astro はデフォルトで JavaScript を送りません。WordPress から来るとここが最大の思考の切り替えポイントです。
インタラクティビティの追加
単純な操作には <script> タグを足します。
<button id="menu-toggle">Menu</button>
<nav id="mobile-menu" hidden>
<slot />
</nav>
<script>
const toggle = document.getElementById("menu-toggle");
const menu = document.getElementById("mobile-menu");
toggle?.addEventListener("click", () => {
menu?.toggleAttribute("hidden");
});
</script>
スクリプトはバンドルされ重複も除かれます。同じコンポーネントがページに 2 回あっても、スクリプトは 1 回実行されます。
応用: インタラクティブコンポーネント
より複雑な操作には、React・Vue・Svelte などをオンデマンドで読み込めます。必須ではなく、多くのサイトは <script> だけで足ります。
---
import SearchWidget from "../components/SearchWidget.jsx";
---
<!-- Only load JavaScript when the search box scrolls into view -->
<SearchWidget client:visible />
| Directive | JavaScript が載るタイミング |
|---|---|
client:load | ページ読み込み直後 |
client:visible | コンポーネントがビューポートに入ったとき |
client:idle | ブラウザがアイドルになったとき |
ルーティング
Astro は ファイルベースのルーティング を使います。src/pages/ のファイルが URL になります。
| File | URL |
|---|---|
src/pages/index.astro | / |
src/pages/about.astro | /about |
src/pages/posts/index.astro | /posts |
src/pages/posts/[slug].astro | /posts/hello-world |
src/pages/[...slug].astro | Any path (catch-all) |
動的ルート
CMS コンテンツには角括弧で動的セグメントを書きます。
---
import { getEmDashCollection, getEmDashEntry } from "emdash";
import Base from "../../layouts/Base.astro";
import { PortableText } from "emdash/ui";
// For static builds, define which pages to generate
export async function getStaticPaths() {
const { entries: posts } = await getEmDashCollection("posts");
return posts.map(post => ({
params: { slug: post.id },
props: { post },
}));
}
const { post } = Astro.props;
---
<Base title={post.data.title}>
<article>
<h1>{post.data.title}</h1>
<PortableText value={post.data.content} />
</article>
</Base>
WordPress との比較
| WordPress | Astro |
|---|---|
Template hierarchy (single-post.php) | Explicit file: posts/[slug].astro |
Rewrite rules + query_vars | File structure |
$wp_query determines template | URL maps directly to file |
add_rewrite_rule() | Create files or folders |
WordPress の概念はどこに対応するか
WordPress 機能の Astro/EmDash 相当を探すための早見表です。
テンプレート
| WordPress | Astro/EmDash |
|---|---|
| Template hierarchy | File-based routing in src/pages/ |
get_template_part() | Import and use components |
the_content() | <PortableText value={content} /> |
the_title(), the_*() | Access via post.data.title |
| Template tags | Template expressions {value} |
body_class() | class:list directive |
データとクエリ
| WordPress | Astro/EmDash |
|---|---|
WP_Query | getEmDashCollection(type, filters) |
get_post() | getEmDashEntry(type, id) |
get_posts() | getEmDashCollection(type) |
get_the_terms() | Access via entry.data.categories |
get_post_meta() | Access via entry.data.fieldName |
get_option() | getSiteSettings() |
wp_nav_menu() | getMenu(location) |
拡張性
| WordPress | Astro/EmDash |
|---|---|
add_action() | EmDash hooks, Astro middleware |
add_filter() | EmDash hooks |
add_shortcode() | Portable Text custom blocks |
register_block_type() | Portable Text custom blocks |
register_sidebar() | EmDash widget areas |
| Plugins | Astro integrations + EmDash plugins |
コンテンツタイプ
| WordPress | Astro/EmDash |
|---|---|
register_post_type() | Create collection in admin UI |
register_taxonomy() | Create taxonomy in admin UI |
register_meta() | Add field to collection schema |
| Post status | Entry status (draft, published, etc.) |
| Featured image | Media reference field |
| Gutenberg blocks | Portable Text blocks |
まとめ
WordPress から Astro への移行は大きいですが筋が通っています。
- PHP テンプレート → Astro コンポーネント — 同じ考え方(サーバーコード + HTML)、整理しやすい
- テンプレートタグ → props と import — グローバルではなく明示的なデータフロー
- テーマファイル → pages ディレクトリ — URL がファイル構造と一致
- フック → スロットとミドルウェア — 挿入点が予測しやすい
- デフォルトで jQuery → デフォルトで JS ゼロ — インタラクションは意図して足す
最初の EmDash サイトは Getting Started から。CMS データの取得と描画は Working with Content を参照してください。