Counting blocks on page

Hi, I've just created a new block for a site I'm developing, but for it to work as intended, it needs to be in a table. What I'd like is for my block to count the number of times it's appearing on the page and if it's 0 or the maximum, echo out the <table> and </table> tags. So in pseudo-code:

* Site owner adds new "Grid item block"
* New "Grid item block" looks for other "Grid item blocks" on the current page.
* If there are none on the page, add a <table> tag to the block
* If there are more than one, then each block does a check. If it's the last one (eg . $gridID = count($gridBlock)), then add the </table> tag.

I hope that made sense and that it's possible. If there's a better way to go about it, do let me know, but the ultimate goal is to have a grid of items, 4 cells wide so the site owner can add new photos into the grid.

View Replies: View Best Answer
jaredquinn replied on at Permalink Reply
This isn't really the way things are designed to work. Blocks are designed to be entirely independent from each other.

Just a thought, but what you are probably better off doing is defining a page template that uses page attributes to determine the structure of a table and create block areas in your grid for adding content to.
andrew replied on at Permalink Best Answer Reply
I'd agree with jaredquinn but I'll offer up some code anyway that is hopefully helpful. From within the block template this should work:

$page = Page::getCurrentPage();
$blocks = $page->getBlocks();
$hasGrid = false;
foreach($blocks as $bl) {
    if ($bl->getBlockTypeHandle() == 'custom_grid_block_type_handle') {
        $hasGrid = true;
if ($hasGrid) {
    print '<table>';


This is just off the top of my head, but I think it should do the trick.