I have a CSV file with about 100 rows and 8 columns. I want to create a “user” for each row. I am attempting to follow this virtual pages guide, and it seems to mostly be going OK aside from the students aren’t being listed.
content -> home -> home.txt
-> home.csv
/site/plugins/helpers/index.php
<?php
function csv(string $file, string $delimiter = ','): array
{
$lines = file($file);
$lines[0] = str_replace("\xEF\xBB\xBF", '', $lines[0]);
$csv = array_map(function($d) use($delimiter) {
return str_getcsv($d, $delimiter);
}, $lines);
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv);
return $csv;
}
/site/models/home.php
<?php
class StudentPage extends Page
{
public function children()
{
$csv = csv($this->root() . '/home.csv', ';');
$children = array_map(function ($student) {
return [
'slug' => Str::slug($student['csenetid']),
'template' => 'home',
'model' => 'home',
'num' => $student->indexOf($students),
'content' => [
'fname' => $student['fname'],
'lname' => $student['lname'],
'nickname' => $student['nickname'],
'csenetid' => $student['csenetid'],
'cohort_yr' => $student['cohort_yr'],
]
];
}, $csv);
return Pages::factory($children, $this);
}
}
/site/templates/home.php
<?php snippet('header') ?>
<main>
<h1><?= $page->title() ?></h1>
<ul class="students">
<?php foreach ($page->children() as $student): ?>
<li>
<a href="<?= $student->url() ?>">
<?= $student->csenetid() ?>
</a>
</li>
<?php endforeach ?>
</ul>
</main>
<?php snippet('footer') ?>
/site/templates/student.php
<?php snippet('header') ?>
<article class="student">
<h1 class="student-csenetid"><?= $page->title() ?></h1>
<p class="student-fname">first name: <?= $page->fname() ?></p>
<p class="student-lname">last name: <?= $page->lname() ?></p>
<p class="student-nickname">nickname: <?= $page->nickname() ?></p>
<p class="student-cohort_yr">cohort_yr: <?= $page->cohort_yr() ?></p>
</article>
<?php snippet('footer') ?>
I am just getting a blank home where the list of children pages should be. What do I have wrong?