Display snippet in subdirectory


#1

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;

define('DS', DIRECTORY_SEPARATOR);

require(__DIR__ . DS . '../kirby' . DS . 'bootstrap.php');

$kirby = kirby();

$kirby->urls->index = 'https://' . $_SERVER['SERVER_NAME'];
$kirby->launch();

snippet('header');

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/.


#2

You would have to pass your variables to the snippet:

snippet('header', ['site' => $kirby->site()]);

#3

The error remains unfortunately, here’s the error in question;


#4

Does the error refer to the $site or the $page variable?


#5

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?


#6

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.

Option 1:

snippet('header', ['site' => $kirby->site(), 'page' => page()]);

Option 2

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>

#7

That’s fantastic, working perfectly now! Thanks for the help, hopefully others will find this useful too.