Forum

Thread tagged as: Question, Problem, Docs

If region has items ?

Hi.

Trying to figure out how to check whether a region has items. If it does then display it otherwise hide that section of html.

Is there any way of saying

if region(‘testimonals’).count = 0 hide block ?

John Griffiths

John Griffiths 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If a region doesn't have any content it won't display by default.

Drew McLellan said:

If a region doesn't have any content it won't display by default.

Yep, but what I've got is a section of html saying "here is a list of our projects" and then after that it displays those projects / regions.

Managed to do it with this:

        $projects = perch_content('project', true);
        echo strlen($projects);

which returns 0 if the region contains no items which means I can hide and show that block when there's content to show.

There's probably a much better way to do this, any pointers?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello John,

I think you can achieve this with:

$projects = perch_content('project', true);

if($projects) {
    echo $projects;
} else {
    // display alternative content
}
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yep, but what I've got is a section of html saying "here is a list of our projects" and then after that it displays those projects / regions.

Put that in the <perch:before> section in your template.

Hussein Al Hammad said:

Hello John,

I think you can achieve this with:

$projects = perch_content('project', true);

if($projects) {
  echo $projects;
} else {
  // display alternative content
}

Excellent!, thanks Hussein