nomad
21
Like
<?php
use Kirby\Cms\Page;
class ItemsPage extends Page
{
public function children()
{
$itemdetail = [];
foreach ($page->items()->toStructure() as $item) {
$itemdetail[] = [
'slug' => $this->slug() . $item->itemnumber(),
'template' => 'sidemenuproducts',
'model' => 'sidemenuproducts',
'content' => [
'title' => $this->title() . '(' . $item->itemnumber() . ')',
]
];
}
return Pages::factory($itemdetail, $this);
}
}
nomad
23
Like this?
<?php
use Kirby\Cms\Page;
class SideMenuProductsPage extends Page
{
public function children()
{
if ($this->children instanceof Pages) {
return $this->children;
}
$itemdetail = [];
foreach ($this->items()->toStructure() as $item) {
$itemdetail[] = [
'slug' => $item->itemnumber(),
'template' => 'sidemenuproducts',
'model' => 'sidemenuproducts',
'content' => [
'title' => $this->title() . ' (' . $item->itemnumber() . ')',
]
];
}
return $this->children = Pages::factory($itemdetail, $this);
}
}
nomad
24
The .txt file is also named correctly, I’m still getting a 404.
nomad
27
If it type in for example the url followed by an existing item number.
trishkaufmann.test/4437
But if I look at your folder structure, the url for that new virtual page should be
items-for-sale/confederate-postal-history/csa-1-on-cover-section-1/4437
Please have a look at how Kirby treats nested content folders url wise.
1 Like
nomad
29
Thank you. That is working and I updated the template within the model so that I can control what is shown.
All that is need on the template is to use the structured fields with the $page variable to pull in the data wanted.
Example
<p><?= $page->itemnumber() ?></p>
nomad
30
Updated Model
<?php
use Kirby\Cms\Page;
class SideMenuProductsPage extends Page
{
public function children()
{
if ($this->children instanceof Pages) {
return $this->children;
}
$itemdetail = [];
foreach ($this->items()->toStructure() as $item) {
$itemdetail[] = [
'slug' => $item->itemnumber(),
'template' => 'itemdetail',
'model' => 'sidemenuproducts',
'content' => [
'title' => $this->title() . ' ' . 'Item#' . ' ' . $item->itemnumber(),
'image' => $item->image(),
'itemnumber' => $item->itemnumber(),
'fulldescription' => $item->fulldescription(),
'price' => $item->price(),
]
];
}
return $this->children = Pages::factory($itemdetail, $this);
}
}
nomad
31
New “Item Detail” template so far
<?php snippet('head') ?>
<?php snippet('header') ?>
<div id="content_container">
<div class="grid-x grid-margin-x">
<div id="page_title" class="cell">
<?= $page->title() ?>
</div>
<div id="page_content" class="cell">
<?= $page->image()?>
<p><?= $page->itemnumber() ?></p>
<p><?= $page->fulldescription() ?></p>
<p><?= $page->price() ?></p>
</div>
</div>
</div>
<?php snippet('footer') ?>
Output
nomad
32
The search now works to find the item detail pages now as well!
nomad
33
Search Template
<?php if ($results->isNotEmpty()): ?>
<strong>Search Results:</strong>
<ul>
<?php foreach ($results as $result): ?>
<li>
<a href="<?= $result->url() ?>">
<?= $result->title()->itemnumber() ?>
</a>
<?= $result->shortdescription() ?>
</li>
<?php endforeach ?>
</ul>
<?php else: ?>
<strong>Results:</strong> <br>
No results found.
<?php endif ?>
<div class="button-group align-center">
<?php if ($results->pagination()->hasPages()): ?>
<?php if ($results->pagination()->hasPrevPage()): ?>
<a class="brand-button" href="<?= $results->pagination()->prevPageURL() ?>">
‹ previous items
</a>
<?php endif ?>
<?php if ($results->pagination()->hasNextPage()): ?>
<a class="brand-button" href="<?= $results->pagination()->nextPageURL() ?>">
more items ›
</a>
<?php endif ?>
<?php endif ?>
</div>
Just styling left to do!