Themes: Obtain number of blocks in area before area is displayed
PermalinkNote: method getArea('AreaName') is not implemented in concrete5
<div class="<?php if ($c->getArea('Sidebar')->getTotalBlocksInArea($c) > 0 || $c->isEditMode()) { ?>with-sidebar<?php } else { ?>fullwidth-no-sidebar<?php } ?>"> <?php // this div can be either full-width or accomodate a sidebar if it has blocks $a = new Area('Main'); $a->display($c); ?> </div> <?php // only display Sidebar if it has blocks or page is in edit mode $a = new Area('Sidebar'); if ($a->getTotalBlocksInArea($c) > 0 || $c->isEditMode()) { ?> <div class="sidebar"> <?php $a->display($c); ?> </div> <?php } ?>
public function getTotalBlocksInArea($c = false) in Area.php returns the total number of blocks in an area.
@param Page $c must be passed if the display() method has not been run on the area object yet.
That code only applies to global areas. How can we get the number of blocks for normal areas?
$a = new Area('Main'); $total = $a->getTotalBlocksInArea($c);
After lots of coffee and some tinkering here's what I have come up with so far
// the div below can be either full-width or reserve space for a sidebar (defined later) that has blocks // css >> with-sidebar{width: 75%;} fullwidth-no-sidebar{width: 100%;} <div class="<?php if (count($c->getBlocks('Sidebar') > 0 || $c->isEditMode()) { ?>with-sidebar<?php } else { ?>fullwidth-no-sidebar<?php } ?>"> <?php // this section can be either full-width or accomodate a sidebar if it has blocks $a = new Area('Main'); $a->display($c); ?> </div> // Conditionally display 'Sidebar' below if it has blocks <?php // only display Sidebar if it has blocks or page is in edit mode $a = new Area('Sidebar'); if ($a->getTotalBlocksInArea($c) > 0 || $c->isEditMode()) { ?> <div class="sidebar"> <?php $a->display($c);
We only show the search area if there are blocks in it or we're in edit mode.