Categories
What it does: Organizes content into subdirectories and applies category-specific templates
Events: POST_GLOB (priority 250), POST_RENDER (priority 100)
How to use: Add a category field to your frontmatter, or create category definition files
Basic Usage
Add a category field to your content frontmatter:
---
title: "Learning PHP Basics"
category: "tutorials"
---
# Learning PHP Basics
Welcome to our PHP tutorial series!
Category Definition Files
Create a category definition file to specify templates for all content in that category:
File: content/tutorials.md
---
type: category
template: tutorial
---
Now all files with category: "tutorials" will automatically use the tutorial.html.twig template.
Template Inheritance
Categories feature applies templates via POST_GLOB event with the following priority:
- File frontmatter template - If file has
template = "xyz", use it - Category template - If file belongs to category with defined template, use it
- Default template - Falls back to base template
This happens automatically during the discovery phase, before rendering.
What Happens During POST_GLOB
- Scan for category definitions - Finds files with
type = "category" - Extract category templates - Stores mapping of category slug → template name
- Apply to content files - Iterates all discovered files and applies category templates
What Happens During POST_RENDER
-
StaticForge sanitizes the category name:
tutorials→tutorialsWeb Development→web-developmentPHP & MySQL→php-mysqlCool_Stuff!→cool-stuff
-
Creates the category directory:
output/tutorials/ -
Moves your file there:
output/tutorials/learning-php-basics.html
Sanitization Rules
- Converts to lowercase
- Replaces spaces and special characters with hyphens
- Removes leading/trailing hyphens
- Keeps only letters, numbers, and hyphens
Double-Nesting Prevention
StaticForge automatically prevents double-nesting when your source directory structure matches your category name:
- Source file:
docs/configuration.mdwithcategory = "docs" - Output:
public/docs/configuration.html(notpublic/docs/docs/configuration.html)
This smart detection ensures clean URL structures when you organize both your source files and categories logically.
Why Use Categories
- Keep related content together
- Create logical URL structures (
/blog/,/tutorials/,/docs/) - Organize large sites into sections
- Enable category-specific styling or templates
Important: This is the only way to create subdirectories in your output. Without categories, all pages go in the root.