|
| 1 | +# @faustjs/astro Template Hierarchy Example |
| 2 | + |
| 3 | +This example demonstrates how to use the `@faustjs/astro` package to automatically discover WordPress templates in an Astro project using the WordPress template hierarchy. |
| 4 | + |
| 5 | +## What This Example Shows |
| 6 | + |
| 7 | +- **Template Discovery**: Automatically finds WordPress template files in your `wp-templates` directory |
| 8 | +- **Content Collections**: Uses Astro's content collections to organize and query templates |
| 9 | +- **Template Hierarchy**: Follows WordPress template hierarchy rules for organizing templates |
| 10 | +- **Template Resolution**: SSR catch-all route that dynamically resolves WordPress URLs to templates |
| 11 | + |
| 12 | +## Project Structure |
| 13 | + |
| 14 | +``` |
| 15 | +src/ |
| 16 | +├── layouts/ |
| 17 | +│ └── WordPressLayout.astro # Shared layout for all templates |
| 18 | +├── content.config.js # Content collection configuration |
| 19 | +└── pages/ |
| 20 | + ├── index.astro # Demo page showing discovered templates |
| 21 | + ├── 404.astro # Custom 404 page |
| 22 | + ├── [...uri].astro # Catch-all route implementing template resolution |
| 23 | + └── wp-templates/ # WordPress template files (protected from direct access) |
| 24 | + ├── index.astro # Home/blog template |
| 25 | + ├── page.astro # Page template |
| 26 | + ├── single-post.astro # Single post template |
| 27 | + └── archive.astro # Archive template |
| 28 | +``` |
| 29 | + |
| 30 | +## How It Works |
| 31 | + |
| 32 | +1. **Template Files**: Place your WordPress templates in `src/pages/wp-templates/` |
| 33 | +2. **Auto-Discovery**: The `createTemplateCollection()` function finds all template files |
| 34 | +3. **Content Collections**: Templates are available as an Astro content collection with `id` and `path` |
| 35 | +4. **SSR Resolution**: The `[...uri].astro` route dynamically fetches content and resolves templates |
| 36 | +5. **Shared Layout**: All templates use `WordPressLayout.astro` for consistent styling |
| 37 | +6. **Template Protection**: Templates can't be accessed directly (e.g., `/wp-templates/page` returns 404) |
| 38 | + |
| 39 | +## Running the Example |
| 40 | + |
| 41 | +```bash |
| 42 | +# Install dependencies (includes GraphQL requirements) |
| 43 | +pnpm install |
| 44 | + |
| 45 | +# Copy environment configuration |
| 46 | +cp .env.example .env |
| 47 | + |
| 48 | +# Edit .env to set your WordPress URL |
| 49 | +# WORDPRESS_URL=https://your-wordpress-site.com |
| 50 | + |
| 51 | +# Start development server (SSR mode) |
| 52 | +pnpm dev |
| 53 | + |
| 54 | +# Build for production (outputs server bundle) |
| 55 | +pnpm build |
| 56 | +``` |
| 57 | + |
| 58 | +## Dependencies |
| 59 | + |
| 60 | +This example requires the following dependencies (automatically installed): |
| 61 | + |
| 62 | +- `@faustjs/astro` - Astro integration with WordPress template hierarchy |
| 63 | +- `@faustjs/template-hierarchy` - Core template hierarchy logic |
| 64 | +- `astro` - Astro framework (peer dependency) |
| 65 | +- `graphql` - GraphQL core library (peer dependency) |
| 66 | +- `graphql-tag` - GraphQL query parsing (peer dependency) |
| 67 | + |
| 68 | +## WordPress Setup |
| 69 | + |
| 70 | +This example requires a WordPress site with WPGraphQL plugin installed: |
| 71 | + |
| 72 | +1. **Install WPGraphQL**: Install the [WPGraphQL plugin](https://www.wpgraphql.com/) on your WordPress site |
| 73 | +2. **Set Environment Variable**: Copy `.env.example` to `.env` and set your WordPress URL |
| 74 | +3. **GraphQL Endpoint**: The endpoint should be `https://your-site.com/graphql` |
| 75 | + |
| 76 | +### Supported WordPress Content |
| 77 | + |
| 78 | +The example automatically discovers and creates routes for: |
| 79 | + |
| 80 | +- **Posts**: Individual blog posts with categories and tags |
| 81 | +- **Pages**: Static pages like About, Contact, etc. |
| 82 | +- **Category Archives**: Archive pages for each category |
| 83 | +- **Tag Archives**: Archive pages for each tag |
| 84 | +- **Home Page**: Main blog index |
| 85 | + |
| 86 | +## Template Discovery |
| 87 | + |
| 88 | +The `@faustjs/astro` package automatically discovers templates in your `wp-templates` directory: |
| 89 | + |
| 90 | +- **index.astro** → Home template |
| 91 | +- **page.astro** → Page template |
| 92 | +- **single-post.astro** → Single post template |
| 93 | +- **archive.astro** → Archive template |
| 94 | + |
| 95 | +Each template is automatically added to the content collection with an `id` and `path`. |
| 96 | + |
| 97 | +## Template Resolution Demo |
| 98 | + |
| 99 | +The catch-all route (`[...uri].astro`) fetches real WordPress content and demonstrates URL resolution: |
| 100 | + |
| 101 | +- `/` → `index.astro` (home page) |
| 102 | +- `/about` → `page.astro` (WordPress page) |
| 103 | +- `/hello-world` → `single-post.astro` (WordPress post) |
| 104 | +- `/category/uncategorized` → `archive.astro` (category archive) |
| 105 | +- `/tag/fun` → `archive.astro` (tag archive) |
| 106 | + |
| 107 | +_Note: Actual URLs depend on your WordPress content_ |
| 108 | + |
| 109 | +## Content Collection Usage |
| 110 | + |
| 111 | +```js |
| 112 | +// Get all templates |
| 113 | +import { getCollection } from 'astro:content'; |
| 114 | +const templates = await getCollection('templates'); |
| 115 | + |
| 116 | +// Find a specific template by id |
| 117 | +const pageTemplate = templates.find((t) => t.id === 'page'); |
| 118 | + |
| 119 | +// Each template has: { id: string, path: string } |
| 120 | +console.log(pageTemplate); // { id: 'page', path: 'wp-templates/page' } |
| 121 | +``` |
| 122 | + |
| 123 | +## Real-World Usage |
| 124 | + |
| 125 | +This example shows how a real WordPress headless site works with SSR: |
| 126 | + |
| 127 | +1. **WordPress GraphQL API**: Dynamically fetches content from WordPress on each request |
| 128 | +2. **Server-Side Rendering**: Renders pages on-demand with fresh WordPress content |
| 129 | +3. **Template Resolution**: Uses WordPress template hierarchy to render content |
| 130 | +4. **Content Types**: Handles posts, pages, categories, and tags dynamically |
| 131 | +5. **SEO Ready**: Generates proper HTML with fresh WordPress content |
| 132 | +6. **404 Handling**: Automatically redirects to 404 for non-existent content |
| 133 | + |
| 134 | +## Learn More |
| 135 | + |
| 136 | +- [WordPress Template Hierarchy](https://developer.wordpress.org/themes/basics/template-hierarchy/) |
| 137 | +- [Astro Content Collections](https://docs.astro.build/en/guides/content-collections/) |
| 138 | +- [@faustjs/template-hierarchy](../packages/template-hierarchy/) |
0 commit comments