How to pass data from virtual pages to a snippet?

Hi
I follow this instructions:

It creates virtual pages to a page.

It works with a multipage website.
But I’m working on a One-pager.

As soon as I add a Model like this, the Website through an unexpected error.
/site/models/pagename.php

<?php

class PagenamePage extends Page
{

    public function children()
    {
        $csv      = csv($this->root() . '/animals.csv', ';');
        $children = array_map(function ($animal) {
            return [
                'slug'     => Str::slug($animal['Scientific Name']),
                'template' => 'animal',
                'model'    => 'animal',
                'num'      => 0,
                'content'  => [
                    'title'       => $animal['Scientific Name'],
                    'commonName'  => $animal['Common Name'],
                    'description' => $animal['Description'],
                ]
            ];
        }, $csv);

        return Pages::factory($children, $this);
    }

}

How can I pass the virtual pages to the snippet of the page?

Kind regards
Alain

What does your loop look like?

You mean this?


or this?

What is $data supposed to be, I’m missing the template where this snippet is called.

And please don’t post code as screenshots, but as code. Thanks.

/site/templates/home.php

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $site->title() ?></title>
    <?= css('assets/css/index.css') ?>
    <?= css('assets/css/gallery.css') ?>

    <!-- fancyapp stuff -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
    <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
    
</head>
<body>


<h1><?= $page->title()  ?></h1>
<ul class="animals">
    <?php foreach ($page->children() as $animal): ?>
    <li>
      <a href="<?= $animal->url() ?>">
        <?= $animal->title() ?>
      </a>
    </li>
    <?php endforeach ?>
</ul>

    <?php 
    snippet('header');
    
    foreach($pages->listed() as $section) {
        snippet($section->uid(), ['data' => $section]);
    }

    snippet('footer');
    ?>
</body>
</html>

It is the /models/offers.php file which breaks the code…

Shouldn’t the /models/offers.php create the virtual pages which then the /snippets/offers.php can call?
Oh I see there is a mistake in /snippets/offers.php in Line 2. It should be $data instead of $page. But $data does not work as well.

Edit:
Ok, found it.
I had to rename the function in models/offer.php from “children” to “anyname” and then I can call that function in snippets/offers.php

But why did php not output a clear error saying “functionname already used” ? (I did turn on debug in config.php).

No, if you want these entries from the csv file to be children of the offers page, the method in the model must be called children() not anyname, that doesn’t make sense. The whole purpose of the model is to overwrite the children() method of the parent model/class.

But exactly, it must be $data->children(), not $page->children() because $page would refer to the calling page, i.e. home.

… but it only works that way. :thinking:

Well, in your last post you didn’t even tell us what the error and the stacktrace was, something like “it breaks” is not really that helpful.

There was only
image
(Debug Mode was turned on).

So it was also not very meaningful to me.
I’m sorry, I was not able to find out more details about the error.

That message doesn’t appear with debug mode on. What does your config file look like?

Is that correct?
/site/config/config.php

return [
    'debug' => true
];

If that’s the only return statement, yes. If you have multiple, no.

It is the only return statement in /site/config/config.php at the moment.

Maybe I have to add something like <?php before?

I was just gonna ask, of course, every php file (with php code in it) needs a php tag. Or maybe I should have said, php code needs a php tag-

Then this is just wrong.

image

I’m sorry, but I will never understand why the documentation is so minimalistic.
For a beginner like me, every char and filepath is important to understand the big picture.

But thanks for your patience.
It helped me a little step forward :slight_smile: