From 16c16fc9d89b9f2a3d3e16b40512947c65335f0b Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 13 Jun 2026 12:03:41 +0300 Subject: [PATCH] Add site data files for templates --- docs/content.md | 20 ++++++ docs/templates.md | 2 + roadmap.md | 1 + src/Build/AuthorPageWriter.php | 2 + src/Build/CollectionListingWriter.php | 1 + src/Build/DateArchiveWriter.php | 3 + src/Build/EntryRenderer.php | 1 + src/Build/NotFoundPageWriter.php | 1 + src/Build/TaxonomyPageWriter.php | 2 + src/Console/BuildCommand.php | 29 +++++++- src/Content/Model/SiteConfig.php | 2 + src/Content/Parser/ContentParser.php | 4 +- src/Content/Parser/SiteConfigParser.php | 5 ++ src/Content/Parser/SiteDataParser.php | 68 +++++++++++++++++++ tests/Unit/Build/EntryRendererTest.php | 27 ++++++++ .../Unit/Content/Parser/ContentParserTest.php | 16 +++++ .../Content/Parser/SiteConfigParserTest.php | 48 +++++++++++++ 17 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 src/Content/Parser/SiteDataParser.php diff --git a/docs/content.md b/docs/content.md index 89320ae..0ec5aec 100644 --- a/docs/content.md +++ b/docs/content.md @@ -27,11 +27,31 @@ content/ │ │ └── john-doe.svg │ ├── john-doe.md │ └── jane-smith.md +├── data/ # Site data files exposed to templates +│ └── company.yaml ├── config.yaml # Site-wide settings (see docs/configuration.md) ├── navigation.yaml # Menu definitions └── standalone-page.md # Standalone page (not in any collection) ``` +## Site Data + +Put YAML files in `content/data/` to expose structured site data to templates. Each `.yaml` or `.yml` file becomes one key in the `$data` template variable using the file name without the extension. + +For example, `content/data/company.yaml`: + +```yaml +name: Acme +links: + - /about/ +``` + +is available in templates as: + +```php + +``` + ## Collections First-level directories under `content/` are **collections** (e.g., `blog/`, `docs/`). diff --git a/docs/templates.md b/docs/templates.md index 6f3c8ea..bc0d0f8 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -111,6 +111,7 @@ All built-in page templates receive these additional variables: | `$uiLanguages` | `list` | Available UI languages exposed by the site | | `$uiCatalogs` | `array>` | Theme UI catalogs for client-side switching | | `$ui` | `YiiPress\I18n\UiText` | Injected localized UI-text helper for bundled theme labels | +| `$data` | `array` | Site data loaded from `content/data/*.yaml` | | `$h` | `Closure(string, int, ?string, bool): string` | Injected alias for `htmlspecialchars()` | | `$t` | `Closure(string, array): string` | Injected shortcut for `$ui->get()` in templates | @@ -119,6 +120,7 @@ Example: ```php