Hi everyone, after scrolling though several posts in the community not giving answer to problem I’m asking for help now.
The idea is to have a Onepager (Home) with generic Sections (Subpages) where editors can add content via custom blocks.
Home (Page)
|—Section A (Subpage)
|——Custom block A
|—Section B (Subpage)
|——Custom block B
|—Section C (Subpage)
|——Custom block C
Problem A
The code used for the menu works fine:
<?php
$items = $pages->children()->listed();
if($items->isNotEmpty()):
?>
<?php foreach($items as $item): ?>
<a<?php e($item->isOpen(), ' class="active"') ?> href="#<?= $item->title()?>"><?= $item->title()?></a>
<?php endforeach ?>
<?php endif ?>
But where I’m stuggeling is how to write the subpage’s title in the section’s id in order to jump to the section when clicking the menu. As it is possible to get all subpages’s titles it should be possible to get only one. But how?
Problem B
Furthermore in each subpage/section there is supposed to be a custom block. Similar situation as above: With the following code I managed to retrieve data from the custom block’s fields but the loop runs through all blocks of all subpages – not only the one where the block is used in.
<?php foreach($page->children() as $subpage): ?>
<?= $subpage->myblocks()->toBlocks() ?>
<?php endforeach ?>
Thanks for your help!
Hello and welcome to the Kirby forum
Problem A
… how to write the subpage’s title in the section’s id in order to jump to the section when clicking the menu. As it is possible to get all subpages’s titles it should be possible to get only one. But how?
You want to list the title of the snippets of Custom block A… as anchor links, right?
The snippets are only intended for the content the parent pages Section A….
You can use the title from the parent pages.
I have solved it like this: In the navigation I use:
<a class="inner-link <?php e($item->isOpen(), ' active') ?>"
href="#<?= Str::slug($item->title()) ?>">
...
And as ID in the respective section:
<section id="<?= Str::slug($data->title()) ?>">
...
Problem B
With the following code I managed to retrieve data from the custom block’s fields but the loop runs through all blocks of all subpages – not only the one where the block is used in.
snippet($section->uid(), [‘data’ => $section]);
What we are doing here is passing the data for each $section
to each snippet. This makes it possible to access all content variables that are stored in the text files.
Therefore, you can no longer use $page in the foreach loop, but
<?php foreach($data->children()->listed() as $project): ?>
<li>
<figure>
<img src="<?= $project->image()->url() ?>" alt="<?= $project->title() ?>">
</figure>
</li>
<?php endforeach ?>
This is described here.
If the one-pager has a simple structure, you don’t need the Custom block A… layer.
Hi @GB_DESIGN, thanks for your aswer. I have tried to use your code and adjust it but it doesn’t work. My coding skills are lacking but maybe I also didn’t make my intention clear enough.
Also my structure scribble was missleading. Another try:
Home (Page - home.php)
|—Section A (Subpage - section.php) containing Custom block A
|—Section B (Subpage - section.php) containing Custom block B
|—Section C (Subpage - section.php) containing Custom block C
No, I want to use the Subpage’s name as id.
An overview on my code so far with comments:
site/templates/home.php
<?= snippet('siteHeader') ?>
<?= snippet('siteNav') ?>
<?php foreach ($page->children() as $subpage): ?> //Works fine: All Subpages of "Home" are displayed
<?php snippet($subpage->intendedTemplate()); ?>
<?php endforeach ?>
<?= snippet('siteFooter') ?>
site/snippets/section.php
<section id="<?=$page->title()?>">//Instead of Parent-Page (Home) here should be the Subpage's title
<?php foreach($page->children() as $subpage): ?>//Instead of displaying all custom blocks of all Subpages show only the custom block used in respective Subpage
<?= $subpage->myblocks()->toBlocks() ?>
<?php endforeach ?>
</section>
site/snippets/siteNav.php (already shown in initial post)
<?php
$items = $pages->children()->listed();
if($items->isNotEmpty()):
?>
<?php foreach($items as $item): ?>
<a<?php e($item->isOpen(), ' class="active"') ?> href="#<?= $item->title()?>"><?= $item->title()?></a>
<?php endforeach ?>
<?php endif ?>
Hope the additional context helps to help me
In your home template, you can pass the subpage as a variable to your snippet:
<?php snippet($subpage->intendedTemplate(), ['subpage' => $subpage]); ?>
or shorter:
<?php snippet($subpage->intendedTemplate(), compact('subpage')); ?>
Then you can use $subpage
inside your snippet.
Hi @lukasbestle, thanks for the tip. I will surly use it someday.
For my question aboved I changed my mind due to timing reasons and I’m working with specific templates for each section now.
Thanks for the support @all!