Hello, I’m trying to use structured field content but I can not get it to show up on the page. Here’s my code:
# /site/blueprints/pages/education.yml
title: Education
icon: book
fields:
education:
label: Education
type: structure
fields:
year:
label: Year
type: text
degree:
label: Degree
type: text
institution:
label: Institution
type: text
// /site/snippets/education.php
<section class="education">
<h2>Education</h2>
<?php $entry = $page->education()->toStructure();
foreach ($entry as $entry): ?> <div class=“entry”>
<span><?= $entry->year() ?></span>
<span><?= $entry->degree() ?></span>
<span><?= $entry->institution() ?></span>
</div> <?php endforeach ?>
</section>
Content file education.txt
Education:
-
year: "2019"
degree: test
institution: test
----
Thank you!
The code should work, if your snippet is actually used on the page that uses the education template. But maybe you are in a different context here.
However, you should never ever use the same variable name for the iterable expression (i.e. the array or collection you loop through) and the value, as this can lead to issues further down the line. So better change to
<?php $entries = $page->education()->toStructure();
foreach ($entries as $entry): ?>
But depending on
the snippet is used on a page that uses the education template.
I changed the variable names. Thanks.
I feel like I’ve checked everything and it still does not work. Any other ideas would be greatly appreciated.
Could you please provide the full code of both the education template and the full education.php snippet?
Hello, thanks for responding!
That is the full education.php snippet.
Its being called on an about.php template
Here’s about.php:
<?php snippet('header') ?>
<?php snippet('menu') ?>
<?php snippet('bio') ?>
<?php snippet('education') ?>
<?php snippet('footer') ?>
Here’s the about.yml:
title: About
title: About
icon:
sections:
parts:
label: Parts
type: pages
status: all
info: “{{ page.intendedTemplate }}”
image: false
templates:
- bio
- education
- solo_exhibitions
That’s exactly the problem here, as already mentioned by @pixelijn, your context is the about page, not an education page, so $page refers to the current page (i.e. your about page) and not the page where your education field is stored.
Use the page helper page('education')
or `page(‘about/education’) (or whatever it is called or the page uuid) to fetch the correct page.