I’d like to display a snippet outside of Kirby’s templates in the header of PHP forum software which resides in a subdirectory on the server (Kirby in the root).
Here’s what I’ve got so far;
require(__DIR__ . DS . '../kirby' . DS . 'bootstrap.php');
$kirby = kirby();
$kirby->urls->index = 'https://' . $_SERVER['SERVER_NAME'];
Unfortunately, this immediately shows an error when the snippet tries to display “$site->title()”. I’m assuming this is because it can’t find the site content or the base path of Kirby might be the subdirectory of the forum instead of the server root?
Just for clarification;
Kirby is in the root of the server, the forum software in /forums/.
You would have to pass your variables to the snippet:
snippet('header', ['site' => $kirby->site()]);
The error remains unfortunately, here’s the error in question;
Does the error refer to the
$site or the
I removed the last bit ($page->title()->html()) and it continues up to the next reference to $page so seems like $page is the issue, how would I fix that? Is there somehow to “fake” a page object and fill in the details?
You could just pass it
page(), which will the refer to the home page (option 1); or you use an if statement in your header, that only tries to output the page title if $page is defined, which probably makes more sense.
snippet('header', ['site' => $kirby->site(), 'page' => page()]);
Do not send send the
$page variable via the snippet and in your header:
<title><?= $site->title()->html() ?> | <?= isset($page)? $page->title()->html(): 'Forum' ?></title>
That’s fantastic, working perfectly now! Thanks for the help, hopefully others will find this useful too.