|
| 1 | +--- |
| 2 | +title: Images and icons in themes |
| 3 | +tags: |
| 4 | + - Plugins |
| 5 | + - Theme |
| 6 | + - Images |
| 7 | + - Icons |
| 8 | +sidebar_position: 2 |
| 9 | +sidebar_label: Images and icons |
| 10 | +--- |
| 11 | + |
| 12 | +One of the theme features is the ability to override any of the standard images within Moodle when your theme is in use. At this point, let's explore how to utilize your own images within your theme and how to override the images being used by Moodle. |
| 13 | +So first up a bit about images within Moodle: |
| 14 | + |
| 15 | +1. Images you want to use within your theme **must** to be located within your theme's `pix` directory. |
| 16 | +1. You can use sub directories within the `pix` directory of your theme. |
| 17 | +1. Images used by Moodle's core are located within the `pix` directory of Moodle. |
| 18 | +1. All plugins should store their images within their own `pix` directory. |
| 19 | + |
| 20 | +The following section assumes that there are two image files in the `pix` directory of a theme named `yourthemename`: |
| 21 | + |
| 22 | +- `/theme/yourthemename/pix/imageone.svg` |
| 23 | +- `/theme/yourthemename/pix/subdir/imagetwo.png` |
| 24 | + |
| 25 | +The first image is an SVG, and the second a PNG located in a subdirectory. |
| 26 | + |
| 27 | +## Use images in templates |
| 28 | + |
| 29 | +The following example illustrates how to make use of these images within your layout file so they can be inserted in your layout template. |
| 30 | + |
| 31 | +```php title="theme/yourtheme/layout/somelayout.php" |
| 32 | +$templatecontext = [ |
| 33 | + $imageone => $OUTPUT->pix_url('imageone', 'theme'), |
| 34 | + $imagetwo => $OUTPUT->pix_url('subdir/imagetwo', 'theme'), |
| 35 | +]; |
| 36 | + |
| 37 | +echo $OUTPUT->render_from_template('theme_yourtheme/somelayout', $templatecontext); |
| 38 | +``` |
| 39 | + |
| 40 | +:::note |
| 41 | + |
| 42 | +A method of Moodle's output library is utilized to generate the URL to the image. It's not too important how that function works, but it is important that it's used, as it's what allows images within Moodle to be overridden. |
| 43 | + |
| 44 | +::: |
| 45 | + |
| 46 | +:::danger Important |
| 47 | + |
| 48 | +**DO NOT** include the image file extension. Moodle will work it out automatically and it will not work if you do include it. |
| 49 | + |
| 50 | +::: |
| 51 | + |
| 52 | +```handlebars title="theme/yourtheme/templates/somelayout.mustache" |
| 53 | +<img src="{{{imageone}}}" alt="Please give your image alt text or set the role to presentation" width="50" height="50"> |
| 54 | +<img src="{{{imagetwo}}}" alt="Please give your image alt text or set the role to presentation" width="50" height="50"> |
| 55 | +``` |
| 56 | + |
| 57 | +## Use images in CSS |
| 58 | + |
| 59 | +The following is how you would use the images from within CSS/SCSS as background images. |
| 60 | + |
| 61 | +```css |
| 62 | +.divone { |
| 63 | + background-image: url([[pix:theme|imageone]]); |
| 64 | +} |
| 65 | + |
| 66 | +.divtwo { |
| 67 | + background-image: url([[pix:theme|subdir/imagetwo]]); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +A placeholder is used within the CSS to allow use of a pix icon. During the CSS/SCSS compilation, these placeholders are converted into a URL which the browser can fetch and serve. |
| 72 | + |
| 73 | +:::note |
| 74 | + |
| 75 | +Notice that the image file extension included. The reason for this leads us into the next topic, how to override images. |
| 76 | + |
| 77 | +::: |
| 78 | + |
| 79 | +## Override images |
| 80 | + |
| 81 | +From within a theme you can **very** easily override any standard image within Moodle by simply adding the replacement image to the theme's pix directory in the same sub directory structure as it is in Moodle. |
| 82 | +So, for instance, if there is a need to override the following images: |
| 83 | + |
| 84 | +1. `/pix/moodlelogo.png` |
| 85 | +1. `/pix/i/user.svg` |
| 86 | +1. `/mod/chat/pix/monologo.svg` |
| 87 | + |
| 88 | +Simply add the replacement images to the theme in the following locations: |
| 89 | + |
| 90 | +1. `/theme/themename/pix_core/moodlelogo.png` |
| 91 | +1. `/theme/themename/pix_core/i/user.svg` |
| 92 | +1. `/theme/themename/pix_plugins/mod/chat/monologo.svg` |
| 93 | + |
| 94 | +:::note |
| 95 | + |
| 96 | +A `pix_core` directory has been created in the theme to store the replacement images. For a specific activity module like chat, the directory `pix_plugins/mod/chat` is needed. This directory is `pix_plugins` and then the plugin type (`mod`) and then the plugin name (`chat`). |
| 97 | + |
| 98 | +::: |
| 99 | + |
| 100 | +Another noteworthy aspect is that Moodle not only searches for replacements of the same image type (svg, png, jpg, gif, and so on) but also replacements in any image format. This is why the image file extension was never specified when working with our images above. |
| 101 | +This means that the following would also work: |
| 102 | + |
| 103 | +1. `/theme/themename/pix_core/moodlelogo.svg` |
| 104 | +1. `/theme/themename/pix_core/i/user.jpg` |
| 105 | + |
| 106 | +For a more detailed description of how this all works see the page on [Using images in a theme](https://docs.moodle.org/dev/Using_images_in_a_theme). |
0 commit comments