Forum
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
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 thestrlen() = 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 usingwhich will return an empty array() with or without saving the region which was just created.
Thats exactly whats wrong...
perch_content() output is cached, so until the region has been saved in admin it will output
<!-- Undefined content: RegionNameHere -->
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:
I check if $help has any content and display/hide the help button accordingly like so:
and output help content like so:
Thanks again for your help.
You could get fancy and do:
:)
Untested and typed on my iPhone
based on your suggestion this actually improves on your original solution
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:
Just a final follow up I had to change this code
to
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.
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!