Forum

Thread tagged as: Question

Determine whether perch_content has any content or not?

ok, so on the majority of the pages in my site there is a hidden section containing help text that is shown / hidden via a link that is elsewhere on the page and not included in my perch code.

So I create the content area like so:

perch_content_create('Help Text for Page', array(
    'template' => 'text_block.html',
    'multiple' => false,
    'edit-mode' => 'listdetail',
));
$help = perch_content('Help Text for Page',true);

and what I would like to do is hide the button if there is no content in $help

Neither:

print(!(empty($help)))?'<a href="#" class="help pure-button" onClick="">Help Panel</a>':'';

or

print($help!= '')?'<a href="#" class="help pure-button" onClick="">Help Panel</a>':'';

works and checking $help the string length i.e.

print(strlen(trim($help)));

the length returned seems to be consistently 46 for an empty field so can I safely use this length as a check or is there another way that I have not found in the docs to achieve this ?

Thanks

Dave Buchholz

Dave Buchholz 0 points

  • 4 years ago

Have you tried <perch:noresults>

https://docs.grabaperch.com/templates/noresults

Robert Ketter said:

Have you tried <perch:noresults>

https://docs.grabaperch.com/templates/noresults

Robert,

that's not going to help me I am afraid, the button I want to hide is not part of my template

Well, if you create a region using perch_content_create(), you still have to save the region before it can be used in perch_content() or perch_content_custom() otherwise the output you will get is<!-- Undefined content: Help Text for Page --> which is the strlen() = 46 your currently getting.

ahh, ok that makes sense. I will look into that, it maybe that the content areas have not all been saved in the admin.

Thanks, that is really helpful.

If you do as I suggested using <perch:noresults> you can set the output in the tag and test for this, or you could test using

$return = perch_content_custom('Help Text for Page', ['skip-template'=>true], true);

which will return an empty array() with or without saving the region which was just created.

Dave Buchholz said:

ahh, ok that makes sense. I will look into that, it maybe that the content areas have not all been saved in the admin.

Thanks, that is really helpful.

Thats exactly whats wrong...

perch_content() output is cached, so until the region has been saved in admin it will output <!-- Undefined content: RegionNameHere -->

Robert Ketter said:

or you could test using

$return = perch_content_custom('Help Text for Page', ['skip-template'=>true], true);

which will return an empty array() with or without saving the region which was just created.

This maybe exactly what I am looking for, I will test this in a little while

Thanks

Robert,

thank you I have a working solution that I am happy with now, that covers all bases whether the region has been set up or not.

so I define my region like so:

perch_content_create('Help Text for Page', array(
    'template' => 'text_block.html',
    'multiple' => false,
    'edit-mode' => 'listdetail',
));
$help = perch_content_custom('Help Text for Page', ['skip-template'=>true], true);

I check if $help has any content and display/hide the help button accordingly like so:

print(count($help) > '0')?'<a href="#" class="showHelp pure-button">Help Panel</a>':'';

and output help content like so:

print $help[0]['text'];

Thanks again for your help.

You could get fancy and do:

if (PerchUtil::count($help)) { ?>
HTML here
<?php }

:)

Untested and typed on my iPhone

based on your suggestion this actually improves on your original solution

print(PerchUtil::count($help[0]['text']) > 0)?'<a href="#" class="showHelp pure-button">Help Panel</a>':''; // display help button if help content exists

and now if the region exists but there is no text the help button is hidden correctly

thank you again

Actually scratch that, it doesn't work quite as intended but this does:

print(count($help[0]['text']) > 0)?'<a href="#" class="showHelp pure-button">Help Panel</a>':''; // display help button if help content exists

Just a final follow up I had to change this code

$help = perch_content_custom('Help Text for Page', ['skip-template'=>true], true);

to

$help = perch_content_custom('Help Text for Page', array('skip-template'=>true), true);

because on my staging server where "nothing" is defined already I was getting an error about an unexpected "["

That's because your staging server is using an old version of PHP. You should make sure your staging server is running the same version of perch as production otherwise you'll be in for many more surprises.

Rachel Andrew

Rachel Andrew 394 points
Perch Support

that sounds like you have PHP5.3 there, note that Perch and Runway 3 will require at least 5.4 (but you want to be ideally on a version of PHP 7).

Thank you Robert & Rachel, you are correct. The clients server (which is where their dev version of the site is located) is running PHP Version 5.3.26 whereas my own server both remote and local are both running PHP 5.5.34.

Another conversation to be had with them then!