Themes: Obtain number of blocks in area before area is displayed

Permalink
How can i get the number of blocks in an area to enable the following construct in my theme

Note: 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.

View Replies:
andrew replied on at Permalink Reply
andrew
Check out how we do it in the Elemental theme in concrete/themes/elemental/elements/header.php.

We only show the search area if there are blocks in it or we're in edit mode.

$as = new GlobalArea('Header Search');
$blocks = $as->getTotalBlocksInArea();
$displayThirdColumn = $blocks > 0 || $c->isEditMode();
?>
            <div class="<? if ($displayThirdColumn) { ?>col-sm-5 col-xs-6<? } else { ?>col-md-8 col-xs-6<? } ?>">
                <?
                $a = new GlobalArea('Header Navigation');
                $a->display();
                ?>
            </div>
            <? if ($displayThirdColumn) { ?>
                <div class="col-sm-3 col-xs-12"><? $as->display(); ?></div>
            <? } ?>
martbase replied on at Permalink Reply
Thanks Andrew,

That code only applies to global areas. How can we get the number of blocks for normal areas?
andrew replied on at Permalink Reply
andrew
It's basically the same, except you pass the $c current page object to the getTotalBlocksInArea() method and use the Area class instead of GlobalArea;

$a = new Area('Main');
$total = $a->getTotalBlocksInArea($c);
martbase replied on at Permalink Reply
In my code sample the $a = new Area('Sidebar') gets defined further down for the right sidebar template. I need to decide whether to reserve space for the sidebar div while creating the 'Main' area and its wrapper div.

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);