Adding Block Regions in Moodle Template
A very quick overview on what you need to add to add a block region in a Moodle theme template.
Config.php
Create a region id name then add it to your ‘regions’ paramater array for the page template (or templates) you want to use that block region on. You’ll need to set a defaultregion if you have a set region.
$THEME->layouts = [
'base' => array(
'file' => 'columns2.php',
'regions' => array(),
),
'frontpage' => array(
'file' => 'frontpage.php',
'regions' => array('side-pre','side-top'),
'defaultregion' => 'side-pre',
),
];
/layout/frontpage.php
You will want to add the following lines of code to this file (or the template file you are adding the region to).
$blockshtml = $OUTPUT->blocks('side-pre');
$topblockshtml = $OUTPUT->blocks('side-top');
$hasblocks = strpos($blockshtml, 'data-block=') !== false;
$hastopblocks = strpos($topblockshtml, 'data-block=') !== false;
In your template context you’ll want to add the following parameters
$templatecontext = [
...
'sidepreblocks' => $blockshtml,
'sidetopblocks' => $topblockshtml,
'hasblocks' => $hasblocks,
'hastopblocks' => $hastopblocks,
...
];
/template/frontpage.mustache
Then in your template mustache file you will want to add the following to display any blocks in that region on your page.
{{#hastopblocks}}
<aside data-region="blocks-column" class="d-print-none topblocks py-1 px-2" aria-label="{{#str}}blocks{{/str}}">
{{{ sidetopblocks }}}
</aside>
{{/hastopblocks}}
/lang/en/theme_name.php
You’ll also want to add a language string for any new block regions you add.
$string['region-side-top'] = 'Top Region';