Complete kirby noob here; having the following code :
$kirby->routes(array(
array(
'pattern' => 'collection/(:any)',
'action' => function($collectionHandle) {
return array(page('collection'), ['ok' => "cool"]);
}
)
));
It took me years to understand that kirby requires a content/collection/collection.txt
file to work.
It won’t matter much to leave a blank file in the content
folder to be able to render this template, but at the same time it makes me feel like it should be done differently. Is there another tool/approach within kirby to be able to render a template without any other requirements than the variables passed to the page ? Without having to create an empty file in content ?
I first landed here (Routes: return template to fake a page - running into error); I have a similar use case, but at the same time, as a template is in use here, I don’t understand either how to pass variables to it, and feed a Response
with the resulting html
Thx a lot for any enlightments about the best way to solve such issue
A page is only a page if the content folder for it exists, otherwise there is just no page object, so page('collection')
will return false if there is no corresponding folder.
While you can load a template in your route action using tpl::load()
, the problem is that you have to pass all variables to that template, including the Kirby object and the site object. And you usually also need a page object, depending on what variables you use in your template, unless you feed the fake page with something else.
It would be helpful to know your exact use case. What sort of content do you want to display in those fake pages and where does the content come from?
Thx a lot for your quick rep
Am getting content from an external api, and those contents are collections of posts, and posts themselves (just showing the collection one above, i do have a similar post/(:any)
too)
Those could easily be produced internally with kirby of course; but here I am, in the need of mixing those sources The idea of importing those into kirby aren’t on the roadmap.
tpl::load()
sounds ok at first, but I obviously want to use existing snippets (header/footer) in those templates, so it’d be preferrable to keep a decent way to feed those with the same base data that would be available in a standard template+content=page context
Would you mind showing me how to correctly pass all the site/kirby objects in a tpl load ?
–
Besides that, I was super pleased to put my hands in a very understandable route>controller>view(page/template)
context here in kirby, but what a surprise to see those are in the end ruled by a simple text file! Still, it might be ‘fake’ within the strong file-based approach kirby is providing; but still, does displaying external data remains acceptable here; or do you consider it is a nasty breach ?
Displaying external data is fine and we will see better options for that in Kirby 3, because we won’t have the strong tie between the file system and page anymore.
But we are still with Kirby 2 for the moment…
As I said, you can pass the Kirby and Site objects to your template, but you will not have a page object. That will presumably cause issues in your standard header and footer snippets, where you usually call the page as well, for example to display a meta title tag.
But let’s get to the template stuff first:
$kirby->routes(array(
array(
'pattern' => 'collection/(:any)',
'action' => function($collectionHandle) {
return tpl::load(kirby()->roots()->templates() . DS . 'template.php', ['kirby' => kirby(), 'site' => kirby()->site()], false );
}
)
));
If you use any snippets in the template and these snippets also use these variables, you have to pass them down to the snippets as well, for example.
snippet('header', ['kirby' => $kirby, 'site' => $site]);
Since you don’t have a page object, you have to use an if-statement in your template/snippets wherever you call a page method and display information from your external source instead.
A single dummy page folder would nevertheless make your life easier…
1 Like
Ok well, Im’ two days old into kirby, tried to make sure that i was not misunderstading that one
imma stick with the dummy; waiting for kirby 3!
thx again a lot for your help and explanations here
regards
This is my personal #1 most wanted feature in the new Kirby 3. It’ll open a whole new box of possibilities ️
–
Right now I also have a few sites which digests data from another source and displays them as “virtual pages”. I also took the hacky approach with the “placeholder-file” in the file system, but you have to duplicate (or if-else nest) a lot of code in snippets (such as navs/language switchers/breadcrumbs/…), exclude those placeholders in sitemaps, search, … . And those “fetched posts” then don’t show up when I want them to show up (e.g. in a site search) without a lot of custom code again.
So I’m very eager to play with this
1 Like