Layout
All themes are required to define the layouts they wish to be responsible for as well as create; however, many layout files are required by those layouts. If the theme is overriding another theme then it is a case of deciding which layouts this new theme should override. If the theme is a completely fresh start then you will need to define a layout for each of the different possibilities.
Note that a new theme that will base itself on another theme (overriding it) does not need to define any layouts or use any layout files if there are no changes that it wishes to make to the layouts of the existing theme.
Layouts are defined in config.php
within $THEME->layouts
.
The following is an example of one such layout definition:
View example of one layout definition in config.php
The first thing Moodle looks at is the name of the layout, in this case it is standard
(the array key in PHP), it then looks at the settings for the layout, this is the theme, file, regions, and default region. There are also a couple of other options that can be set by a layout:
theme
[ Optional ]. Is the theme the layout file exists in. That's right: you can make use of layouts from other installed themes.file
[ Required ]. Is the name of the layout file this layout wants to use.regions
[ Required ]. Is the different block regions (places you can put blocks) within the theme.defaultregion
[ Required if regions is non-empty, otherwise optional ]. Is the default location when adding new blocks.options
[ Optional ]. An array of layout specific options described in detail below.
The theme is optional. Normally the layout file is looked for in the current theme, or, if it is not there, in the parent theme. However, you can use a layout file from any other theme by giving the theme name here.
You can define whatever regions you like. You just need to pick a name for each one. Most themes just use one or both of side_pre
and side_post
, which is like 'left side' and 'right side', except in right to left languages, when they are reversed. If you say in config.php
that your the layout provides regions called fred
and barney
, then you must call $OUTPUT->blocks_for_region('fred')
and $OUTPUT->blocks_for_region('barney')
somewhere in the layout file.
The final setting options is a special case that only needs to be set if you want to make use of it. This setting allows the theme designer to specify special options that they would like to create that can be later accessed within the layout file. This allows the theme to make design decisions during the definition and react upon those decisions in what ever layout file is being used.
One such place this has been used is within the boost theme. If you take a look first at theme/boost/config.php
you will notice that several layouts specify options langmenu
and nonavbar
which can both be set to either true or false. The layout options can then be used on the layout .php
files, mustache templates and renderers.
<?php
$hasnavbar = (empty($PAGE->layout_options['nonavbar']) && $PAGE->has_navbar());
$hasfooter = (empty($PAGE->layout_options['nofooter']));
Layout files
Layout files are used to provide a different layout of the elements of the page for different types of pages in Moodle.
In the config.php
for a theme there is a list of layouts which map a page type to a specific PHP page in the layout folder for the theme.
View example of layout definition for popup
It is possible to implement a layout file directly in PHP by echoing the HTML for the page, or mixing PHP tags with HTML, but a better way to create a layout file is to gather all the information required for the layout into a context and render it with a mustache template.
Using templates for layout files makes a lot of sense because they are easier to read and maintain than mixing PHP and HTML in the same file.
View example of a layout file using a template
View example of the mustache template for the previous layout
If we had block regions in this layout we would need to insert them in the template. The way we would do this is by getting the HTML for the block region in our layout PHP file, adding it to the context and then including it in our template.
$blockshtml = $OUTPUT->blocks('side-pre');
$hasblocks = strpos($blockshtml, 'data-block=') !== false;
...
$templatecontext = [
...
'sidepreblocks' => $blockshtml,
'hasblocks' => $hasblocks,
...
];
echo $OUTPUT->render_from_template('theme_boost/columns2', $templatecontext);
When writing layout files, consider the variations in layouts and how the HTML utilized in each may differ. It is often unnecessary to create a distinct layout file for every layout; instead, existing layout files can often be reused across multiple layouts. Additionally, employing layout options can further minimize the need for creating additional layout files.
It's important to note again that when customizing an existing theme, the creation of layouts or layout files may not be necessary.
Layout types
base
. Most backwards compatible layout without the blocks. This is the layout used by default.standard
. Standard layout with blocks. This is recommended for most pages with general information.course
. Main course page.coursecategory
. Use for browsing through course categories.incourse
. Default layout while browsing a course, typical for modules.frontpage
. The site home page.admin
. Administration pages and scripts.mycourses
. My courses page.mydashboard
. My dashboard page.mypublic
. My public page.login
. The login page.popup
. Pages that appear in pop-up windows (no navigation, no blocks, no header).frametop
. Used for legacy frame layouts only. No blocks and minimal footer.embedded
. Used for embedded pages, like iframe/object embedded in moodleform. It needs as much space as possible.maintenance
. Used during upgrade and install. This must not have any blocks, and it is a good idea if it does not have links to other places. For example there should not be a home link in the footer.print
. Used when the page is being displayed specifically for printing.redirect
. Used when a redirection is occurring.report
. Used for reports.secure
. Used forsafebrowser
andsecurewindow
.