Forum

Thread tagged as: Problem

Perch Variables and perch_content_custom 'each'

Hi guys,

I'm having some issues with setting Perch Variables inside a perch_content_custom call, and passing them through into a template.

I need to list out the names of pages along with data from a region on those pages in a list. Below is my code:

<?php
        $page_url = perch_page_url([
            'include-domain' => false,
        ],true).'*';
      perch_content_custom('house details', [
        'page'=>$page_url,
        'sort'=>'plot',
        'sort-order'=>'DESC',
        'template'=>'_availability_pricing.html',
        'each'=>function($house){
            $page_title = perch_pages_navigation([
                'from-path'=>$house["_page"],
                "include-parent"=>true,
                'template'=>'_page-title.html'
            ],true);
            var_dump($page_title);
            PerchSystem::set_var('htb_page-title',$page_title);
            print_r(PerchSystem::get_vars());
            return $house;
        }
      ]);
?>

The issue is that when the perch_content_custom call renders, it only shows the most recent value for htb_page_value. Please see below for my template code.

_availability_pricing.html:

<perch:before>
<div class="availability-prices">
<h2>availability &amp; prices</h2>
<table>
  <tr>
    <th>plot</th>
    <th>house style</th>
    <th>bedrooms</th>
    <th>availability</th>
    <th>price</th>
    <th>&nbsp;</th>
  </tr>
    </perch:before>
      <tr>
      <td><perch:content id="plot" type="text" label="Plot number" /></td>
    <td><a href="<perch:content id="_page" />"><perch:content id="htb_page-title" /></a></td>

      <td><perch:content id="beds" type="text" /></td>

          <td>  <perch:content id="availability" type="radio"/></td>

      <td>&pound;<perch:content id="price" type="text" /></td>
      <td><a href="" class="view-btn">view</a></td>
    </tr>
<perch:after>
</table>
</div>
<!-- /.availability-prices -->
</perch:after>

All other info is coming through as expected.

Any ideas?

Darren Cusdin

Darren Cusdin 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

set_var() isn't going to help in that context - the values have already be retrieved from it by the time the closure is executed. The idea of being passed the item into the closure is that you can both read and modify it before handing it back. Change this:

PerchSystem::set_var('htb_page-title',$page_title);

to:

$house['htb_page-title'] = $page_title;