Hi all,
I’m using this amazing and powerful Kirby + Vue.js Starterkit in combination with Kirby Builder.
I’m trying to adapt @timoetting’s foreach loop for the Kirby Builder blocks into the json.php templates that the starterkit uses.
Here’s the standard PHP foreach loop:
foreach($page->projectBlocks()->toBuilderBlocks() as $block):
snippet('blocks/' . $block->_key(), array('data' => $block));
endforeach;
In my project.json.php
file, here’s what is working, without the builder loop:
<?php
$data = [
'url' => $page->url(),
'title' => $page->title()->value(),
'heroImage' => $page->heroImage()->toFile()->url(),
'overview' => $page->overview()->value()
];
echo json_encode($data);
Yeah! I can render all of the above in my Vue.js files on the frontend, but now the issue comes in when I attempt to use the builder loop…
Please note, I suck with PHP haha, I’m a frontend dev, zero PHP experience, I’m just trying by example here. So here’s two things I tried:
<?php
$data = [
'url' => $page->url(),
'title' => $page->title()->value(),
'heroImage' => $page->heroImage()->toFile()->url(),
'overview' => $page->overview()->value(),
'builderBlocks' => array_values($page->projectBlocks()->toBuilderBlocks()->map(function ($block) {
$block = snippet('blocks/' . $block->_key(), array('data' => $block));
return [
'block' => $block
];
})->data())
];
echo json_encode($data);
Note: The above breaks, I just get a 404 Error page. Since the Vue.js starterkit is running with npm run dev
it appears to be running its own PHP server on 127.0.0.1:8000
but the API is running on localhost:3000
so I’m completely lost in how to show/log PHP errors (this isn’t your typical php -S localhost:8000
running with MAMP or whatever) and yes, I turned debugging on/off in Kirby config, no luck though…
The above forloop of array_vlaues was copied from how the Starterkit was doing things from the site/templates/home.json.php example here.
Now here’s the other thing I tried:
<?php
$data = [
'url' => $page->url(),
'title' => $page->title()->value(),
'heroImage' => $page->heroImage()->toFile()->url(),
'overview' => $page->overview()->value(),
'builderBlocks' => array_values($page->projectBlocks()->toBuilderBlocks()->map(function ($block) {
return $block->_key()->value();
})->data())
];
echo json_encode($data);
If I just return the block key value, this doesn’t break, and my frontend code just outputs the value as a string. I’m using two builder blocks, so it renders each out by the key:
single_video_loop
split_text_block
The above values are also in my blueprint, and the name of the templates builder block file just like Tim’s documentation said to do (and I used that back in Kirby 2 which totally worked as well, so the naming convention I have is all set up correctly as far as I know).
Happy to provide more details / examples if need be (like my blueprint setup, etc).
Thank you all for reading through this and helping out in advance!