Undefined variable $item

Hello all,

I’ve just modified a page blueprint to include phone number and email contact details.

The page blueprint looks like this:

title: Services
icon: star

image:
  query: false
  back: black

options:
  changeStatus: true
  changeTemplate: true
  delete: false

columns:
  main:
    width: 2/3
    fields:

      description:
        label: Description (meta)
        type: text

      navigation:
        label: Navigation label
        type: text

      heading:
        label: Heading (H1)
        type: textarea

      intro:
        label: Intro
        type: textarea

      phone:
        label: Phone Number
        type: tel
        required: false
        width: 1/2
      email:
        label: Email Address
        type: email
        required: false
        width: 1/2

      servicesoptions:
        label: Services Modules
        type: structure
        columns:
          title:
            width: 1/4
          text:
            width: 3/4
        fields:
          title:
            label: Title
            type: text
          icon:
            label: Icon
            type: files
          text:
            label: Text
            type: textarea
          url:
            label: Url
            type: text
            required: false
          linktext:
            label: Link Text
            type: text
            required: false

      text:
        label: Supplemental text
        type: textarea
        size: large

  sidebar:
    width: 1/3
    sections:
      files:
        headline: Files
        type: files
        image:
          back: white

The new fields items are ‘Phone Number’ and ‘Email Address’.

I’ve added the following code into my template:

          <?php if ($item->phone()->isNotEmpty()): ?>
            <div class="plate-contact">
              <a href="tel:<?= Str::slug($item->phone()->html()) ?>" class="btn"><?= $item->phone()->html() ?></a>
         			<?php endif ?>
         <?php if ($item->email()->isNotEmpty()): ?>
              <a href="mailto:<?= $item->email()->html() ?>" class="btn"><?= $item->email()->html() ?></a>
         <?php endif ?>
      </div>

However, I’m getting an ‘Undefined variable $item’ error when I go to the page.

What’s particularly puzzling is that exactly the same template code works on one of my other pages, the only difference is that the phone number and email addresses are included in the blueprint as part of a structure field.

Can someone tell me what’s causing the error?

What is $item? Could you please provide more context?

That’s a good question Sonja. :thinking:
I just realised that while $item is a defined variable on the page I pulled the code from, it has no definition or meaning on my new page. If I change $item to $page the code works as it should.

Solved (and thanks for making me think!) :wink: