3 use BookStack\Entities\Models\Page;
4 use BookStack\Facades\Theme;
5 use BookStack\Theming\ThemeEvents;
7 // Listen to page include parsing events
8 Theme::listen(ThemeEvents::PAGE_INCLUDE_PARSE, function (string $tagReference, string $replacementHTML, Page $currentPage, ?Page $referencedPage) {
10 // Allow default behaviour for non-tag-based includes
11 if (!str_starts_with($tagReference, '0tag:')) {
15 // Get the target tag name from the include reference
16 $tagName = explode(':', $tagReference)[1];
18 // Fetch the tag value from the page, parent chapter, or parent book
19 $tagValue = $currentPage->tags()->where('name', '=', $tagName)->first()?->value ??
20 $currentPage->chapter?->tags()->where('name', '=', $tagName)->first()?->value ??
21 $currentPage->book?->tags()->where('name', '=', $tagName)->first()?->value ?? '';
23 // Return the tag value to be used for the include