Kirby 2 to 3 update

I’m trying to update the last Kibry 2 website to 3, since I can’t longer stay on PHP 7.4.

An a template based website I’m getting the error:

Whoops \ Exception \ ErrorException (E_WARNING)
Undefined variable $file

This line seems to cause it:

    snippet($section->section()->value(), array('section' => $section));

Hope there is an easy fix. The rest seems to work so far.

Please provide more information, particularly the code where the $file variable is used (if it comes from the snippet, the culprit is probably inside the snippet).

The $file part actually from inside Kirby

/Applications/MAMP/htdocs/oldenburg/kirby/config/components.php

    'snippet' => function (App $kirby, $name, array $data = []): string {
        $snippets = A::wrap($name);
 
        foreach ($snippets as $name) {
            $name = (string)$name;
            $file = $kirby->root('snippets') . '/' . $name . '.php';
 
            if (file_exists($file) === false) {
                $file = $kirby->extensions('snippets')[$name] ?? null;
            }
 
            if ($file) {
                break;
            }
        }
 
        return Snippet::load($file, $data);

The snippet is:

<?php snippet('header') ?>

<?php foreach($pages->children()->listed() as $section):

    snippet($section->section()->value(), array('section' => $section));

endforeach; ?>

<?php snippet('map') ?>
<?php snippet('footer') ?>

Oh, ok, then what you pass to the snippet function (i.e $section->section()->value())
seems to return null instead of a filename.

Yep, I was wondering about the $section->section()
It is a Kirby 2 template and worked there.
The sections are listed. How would I have to address them in Kirby 3?

This part is fine.

But the question is what this returns. You have to make sure that this doesn’t return null. The snippet method expects a string or an array as parameter.


However, I will create an issue on GitHub, because the undefined variable error shouldn’t happen and should be catched.

Edit: This is fixed in 3.9.1

I tried to replace it with the way the OnePager tutorial recommends to get sections.

<?php foreach($pages->children->listed() as $section): ?>
<?php snippet($section->uid()) ?>
<?php endforeach ?>

But it also returns null
Maybe the issue is elsewhere.

This line has now an error, must be children() not children, but that was correct in your example above.

I assume $section correctly returns a Page object, but you haven't anwered my question what $section->section()->value()` returns. Disable the call to snippet and output

<?php foreach($pages->children()->listed() as $section):
var_dump($section->section()->value());
    //snippet($section->section()->value(), array('section' => $section));

endforeach; ?>

instead.

This gets you all pages of your site, not just the first level pages, and I somehow doubt this is what you want and probably not all pages have that section() field defined.

You probably want either $page->children()->listed() or $pages->listed()

Sorry for not answering the question, I don’t know the answer.
I’m trying to fix a Kirby 2 theme that I implemented 6 years ago and did not write.

But the fixed code might lead us to it.

/Applications/MAMP/htdocs/oldenburg/site/snippets/about.php

<section id="<?php echo $section->anchor()->value; ?>">

All sections are on the second level.
That’s probably, why this code is unexpected.

Screenshot 2023-01-14 at 20.24.34

If I disable the call to snipped, I’m getting

NULL NULL NULL NULL NULL NULL NULL

As you assumed.

Ok, let’s go a step back. Where do you call the snippets? In the home template?

Sorry I called it the snipped above.
But it is actually the home.php calling the snippets.

<?php snippet('header') ?>

<?php foreach($pages->children()->listed() as $section):

    snippet($section->section()->value(), array('section' => $section));

endforeach; ?>

<?php snippet('map') ?>
<?php snippet('footer') ?>

On the working Kirby 2 website, those values return the lowercase page title.
These are also used in the menu.

<section id="about">

I tried to replace all these ->value() calls with ->uid() but they also return NULL

I did also try the $page->children()->listed() part. Since it indeed seems more logical. But I get the same results.

Ok, in your home.php you want to loop through the children of the home page, so it must in any case be

<?php foreach ($page->children()->listed() as $section): ?>
  <?php snippet($section->section()->value(), ['section' => $section]);
<?php endforeach ?>

But again, what is section(), is that a field on those children pages? Or a custom page method?

I think we could solve this faster if you send me a link to a zip of your project via PM.

1 Like

The problem was caused by a mismatch of default language and content file language code extensions, resulting in empty field values being passed to the snippet helper.

Thank you so much, like every time you helped me so far, at late hours and even during the weekend! :bouquet:
This was the last one. All K3 now.