Hello,
I have a feeling I may be attempting the impossible here, but I shall ask anyway!
Part of the site Iām building has many 1000ās of pages containing a certain type of content, with data of a length and format that would be easier to manage in a database. I have successfully transferred this data from the existing flat content files, into a SQLite database, and via a route, been able to query it, and populate a template.
// config/config.php
array(
'pattern' => 'stations/(:any)',
'method' => 'GET',
'action' => function ($uid) {
$db = new Database(array(
'type' => c::get('db.type'),
'database' => c::get('db.database')
));
$stations = $db->table('stations');
$station = $stations->where('uid', '=', $uid)->first();
if (!$station) {
return site()->errorPage();
} else {
tpl::load(kirby()->roots()->templates().DS.'station.php', [
'site' => kirby()->site(),
'page' => $station
], false);
}
}
)
Youāll not that Iām pointing $page
to the $station database row. This means, that in my template, I can write the following:
// templates/station.php
<?= $page->title() ?>
The problem Iām running into now, as I further integrate this data into the rest of my site, is that as soon as I include any snippets or patterns that use queries like empty()
, I get errors such as:
Call to a member function empty() on null
Or when I use $page->isHomePage()
, my template breaks. In both cases I think Kirby is expecting a true
or false
value to be returned, but Iām returningā¦ Iām not sure, but null
, I suspect.
I thought this issue could be solved by creating a new page model (StationPage
), and populating this with the values my snippets expect. Yet, from my explorations, it looks as though routes donāt touch page models if thereās no corresponding content file. Is that correct?
Anyway, what Iām hoping to achieve, is to create the page object that Kirby expects, but āmergedā with data obtained from a database (and my own hard-coded values), all without needing to create corresponding content files. Is this possible? Please, please say it is!!
Thanks,
Paul