Forum

Thread tagged as: Question, Problem

Get Content From Another Page

Hi guys,

I'm trying to use perch_content_custom() to pull information from a region on another page, but nothing is being returned.

default.php:

<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); ?>
<?php perch_layout('global.header'); ?>
<?php perch_page_attributes(); ?>
<?php 
$resources = perch_content_custom('test',array(
    'page'=>'/res_centre.php',
    'template'=>'resource_centre.html'
));
PerchSystem::set_var('resources_returned',$resources);
?>
<div class="innerwrap">
  <?php perch_content('header-image'); ?>
  <div class="content-wrapper">
    <div class="content-area">
      <div class="left-col">
        <h3 class="turquoise">more in this section</h3>
        <nav class="sidebar">
          <?php 
    perch_pages_navigation(array(
        'from-path' => '/solutions',
        'levels'    => 2
    ));
?>
        </nav>
      </div>
      <!-- /div.left-col -->
      <div class="page-template">
        <?php 
            var_dump($resources);
            print_r(PerchSystem::get_vars()); 
        ?>
        <?php perch_content('page template'); ?>
      </div>
      <div class="clearfloat"> </div>
      <!-- /div.clearfloat --> 

    </div>
    <!-- /div.content-area --> 
  </div>

  <!-- /div.content-wrapper --> 

</div>
<!-- /div.innerwrap -->

<?php PerchUtil::output_debug(); ?>
<?php perch_layout('global.footer'); ?>

The $resources variable is coming back NULL.

res_centre.php:

<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); ?>
        <?php perch_content_create('test',array('template'=>'resource_centre.html')); ?>
<?php perch_layout('global.header'); ?>
<?php perch_page_attributes(); ?>
<div class="innerwrap">
  <?php perch_content('header-image'); ?>
  <div class="content-wrapper">
    <div class="content-area">
      <div class="page-template">
        TEST TEST TEST<?php perch_content('resource centre'); ?>
        <?php perch_content_custom('test'); ?>
      </div>
      <div class="clearfloat"> </div>
      <!-- /div.clearfloat --> 

    </div>
    <!-- /div.content-area --> 
  </div>

  <!-- /div.content-wrapper --> 

</div>
<!-- /div.innerwrap -->
<?php perch_layout('global.footer'); ?>

There are items in the 'test' region.

Any ideas? Thanks!

Harry Ray

Harry Ray 0 points

  • 4 years ago
Simon Clay

Simon Clay 127 points

Just checking your content is definitely in 'test' not 'resource centre'?

Simon Clay

Simon Clay 127 points

Also, try adding , true to the perch_content_custom call to return the value, rather than output it.

$resources = perch_content_custom('test',array(
 'page'=>'/res_centre.php',
 'template'=>'resource_centre.html' 
), true);

Hi Simon,

I found that answer (including ,true) in another thread, but thanks for that!

The issue I'm having now is that the variable isn't outputting anything.

Using get_vars() shows that the variable is populated, but using it in the HTML template through <perch:content id="resource-centre" isn't outputting anything.

Any ideas on that?

Simon Clay

Simon Clay 127 points

In your page you've used PerchSystem::set_var('resources_returned',$resources);

So you'd need to use resources_returned in your template:

<perch:content id="resources_returned">

Hi Simon,

Sorry - typo! I'm using:

<h3 class="turquoise">Resources</h3>
                <p>TEST - REPUBLISH
                <perch:content id="resources_returned" /></p>

And nothing's coming back - TEST - REPUBLISH outputs, but nothing else.

get_vars() displays the expected output under the name ["resources_returned"] so I'm not sure what's going wrong.

Simon Clay

Simon Clay 127 points

In your page code above, I can see you're setting the variable, but I can't see where you're trying to output the template that contains <perch:content id="resources_returned" />

No worries - this is how that section of my PHP Page looks:

<?php 
            $resources = perch_content_custom('test',['page'=>'/res_centre.php','template'=>'resource_centre.html'],true);
            PerchSystem::set_var('resources_returned',$resources);
            perch_content('page template');
            var_dump(PerchSystem::get_vars()); ?>

(obviously, 'page template' is the HTML template that I'm trying to output to.)

Drew McLellan

Drew McLellan 2638 points
Perch Support

Change:

perch_content('page template');

to

perch_content_custom('page template', []);

Hi Drew!

Thanks for the help there - one final question; this is now outputting as a string rather than rendering as HTML.

Any tips?

Simon Clay

Simon Clay 127 points

Add encode="false" to the template tag.

<perch:content id="resources_returned" encode="false" />

Sorry, sorted it!

<perch:content id="resources_returned" html="true"/>

Thanks for the help both!