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
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.
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.
Thanks Andrew,
That code only applies to global areas. How can we get the number of blocks for normal areas?
That code only applies to global areas. How can we get the number of blocks for normal areas?
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);
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
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);
Viewing 15 lines of 18 lines. View entire code block.
We only show the search area if there are blocks in it or we're in edit mode.