Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Using images in a theme: Difference between revisions

From MoodleDocs
No edit summary
m (Changed "themes" to "theme's" in several places. The apostrophe indicates the item belongs to the theme. For example, "the theme's directory" should be used, rather than "the themes directory".)
 
(63 intermediate revisions by 20 users not shown)
Line 1: Line 1:
{{Template:Themes}}{{Moodle 2.0}}Hello, this document looks at how to use images within your own theme. It addresses the two main aspects to using images: first adding new images to your theme, and second how to override existing images within Moodle.
{{Template:Themes}}
 
==A little background==
==A little background==
Moodle 2.0 has introduced a new `better` method of making use of images within its pages. For you as a themer this new method ensures that you can always take full control over what images are being used, can easily introduce new images into your themes, and when possible ensures the images you use are passed through Moodle's new performance caching system so that those who use your theme will get the best performance possible.
Moodle has a standard way of making use of images within its pages enabling theme designers to take full control over what images are being used and when possible ensures images are passed through Moodle's performance caching system to obtain the best possible performance.
 
Those of you who are familiar with Moodle 1.9 or lower will more than likely be familiar with both $CFG->themewww and custom pix. These are both gone from Moodle 2.0 and have been replaced by this new system which is a little like custom pix. Take the time to learn this new system and we are sure you will appreciate it.
 
==Why use a special system for images==
For those of you who are new to Moodle you are probably asking why we have created a special system for images? The answer can be summed up by two concepts diversity and flexibility.
 
===Diversity===
First up diversity, Moodle is used hundreds of countries around the world, there are thousands of installations, millions of end users, every single installation out there is going to be unique in some way. Add to that the brilliant efforts of the community in coming up with plugins, customisations, hacks, and support products and you require a system that is truly diverse.
 
Images need to work no matter what the URL or URI of the installation is, whether it is running over http or https, and whether it is coming from Moodles core image stock or from a plugin or customisation.
 
Other things such as performance also need to be taken into consideration, some people use Moodle on local fibre networks, some over broadband, some still use dial up. Moodles system is designed to make sure we deliver images with the best performance possible.
 
===Flexibility===
The other key concept in the image system is flexibility. This is there for all the themers out there, and there are a lot. Not only do we want to be able to serve images intelligently we also want them to be over-rideable so that the people of are making the look and feel that the millions of end users will experience can create the solution they desire.
 
It shouldn't matter where the image is coming from they should be able to override it and use there own. This system aims to achieve just that.
 
However there will be images that can not be overridden, old code being the culprit in this case, but do not fret we have put a great deal of effort into making sure that all core code uses this new system and while there will be places we have missed hopefully they are few and far between.
 
==Before we start==
==Before we start==
There's not too much to this at the end of the day however a little preparation at this point will ensure I don't have to repeat instructions later on :).
# Make sure you have a theme and images to work with, one that you have a backup of just in case.
So:
# Take note of your theme's main layout file e.g. ''standard.php''.
# Make sure you have a theme to work with, one that you have a backup of just encase.
# Take note of your theme's main CSS file e.g. ''core.css''.
# Have the images you want to use ready to go.
# Turn on '''Theme designer mode'''.
# Take note of your themes main layout file, for the purpose of this document I will assume it is ''standard.php''.
# Take note of your themes main CSS file, I'm going to use ''core.css'' for this document.
# Turn on '''Theme designer mode''' because if its off you won't see any of your changes!
And that's it!
 
==Image locations within Moodle==
==Image locations within Moodle==
This is most pertinent to those looking to override images as in order to override the image you must understand where the image is coming from. If you can't find it you can't override it.
There are three main areas in which images are located - core, plugin and theme.
Within Moodle there are three main areas in which images will be located:
 
; core : These are images that are stored within Moodle's pix directory ''moodle/pix'' and are used all throughout Moodle.
; plugin : Images used by plugins are stored somewhere within the plugin's individual directory. e.g. mod/forum/* or blocks/navigation/*
; theme : This is the most relevant to you as a themer, images for theme's will be stored in a sub directory of a theme.
 
So lets look at each of the main areas:
 
===Core images===
Core images as mentioned above are images that are used all throughout Moodle. They are located within Moodle's pix directory and for many of them within a subdirectory of the main pix directory.
 
If you don't know where to find an image then this is probably the best place to start.
 
If you open the pix directory within your file explorer you will see that there are half a dozen sub directories all of which contain many images. The following is a list of the directories you will find in there an what each contains.
* '''a''' Contains a handful of icons that are not widely used.
* '''c''' Calendar related icons.
* '''f''' File icons for different file types.
* '''g''' Default user icons and thumbnails
* '''i''' General icons
* '''m''' Currency symbols
* '''s''' Smileys
* '''t''' General icons
* '''u''' User icons and thumbnails
* '''y''' YUI icons
 
Keep in mind when overriding these icons that they get used all over the place, don't assume the one place you are seeing them is the only place they are used.


===Plugin images===
Core images are used throughout Moodle and are stored in ''moodle/pix''.
Plugin images are images that are used been used by specific plugins and will always be located within the specific plugins directory. There a dozens of different plugin types within Moodle however  you will find that most of the time only the following will use images:
* Modules: Activities and resources located in the '''/mod/''' directory
* Blocks: Located in the '''/blocks/''' directory
The [[Plugins|plugin types]] page contains a full list of plugin types and links off to information on each.


===Theme images===
The pix directory contains the following subdirectories:
These are images that are used solely by themes and is what you will be using when you read the section on [[#Adding new images to your theme|adding new images to your theme]].
:''a'' - Icons that are not widely used
:''c'' - Calendar-related icons
:"e" - Editor-related icons
:''f'' - File icons for different file types
:''g'' - Default user icons and thumbnails
:''i'' - General icons
:''m'' - Currency symbols
:''s'' - Smileys
:''t'' - General icons
:''u'' - User icons and thumbnails
:''y'' - YUI icons
Plugin images are used by plugins and are stored within the plugin's directory e.g. ''mod/forum/*'' or ''blocks/navigation/*''


Theme images are used in themes and are stored in the ''pix'' subdirectory of a theme.
==Adding new images to your theme==
==Adding new images to your theme==
Here we are looking at introducing a new images for use within your theme. These will be things such as background images, or logos and can be used in both CSS and within the HTML of your theme.
To add new images to your theme, such as a background image or a logo, you should start by creating a pix directory within your theme's directory.
 
The first thing to do is create a pix directory within your themes directory. This is where all of your own images are going to live.
 
For the purpose of this document lets assume you have three images that you want to use.
 
# Copy '''gradient.png''' into your themes  pix directory.
# Copy '''logo.jpg''' into your themes pix directory
 
The plan here is to use gradient.png as the background image for your theme, this we will do in CSS. After that we will put logo.jpg into the header for your theme which we will do in your theme's layout file.


===Using your images within CSS===
Lets assume you have two images, gradient.png and logo.jpg. Copy both files to your theme's pix directory.
Using images within CSS is very easy. As you're all undoubtedly aware Moodle parses all CSS files. When theme designer mode is off Moodle combines them into a handful of large CSS files that get cached and served. Well at the same time Moodle also looks at the CSS and replaces special syntax rules.  


The plan is to use gradient.png as the background image for your theme (in CSS) and use logo.jpg in the header (in your theme's layout file).
===Using images within CSS===
Moodle parses all CSS files. When theme designer mode is off, Moodle combines them into a handful of large CSS files that get cached and served. At the same time Moodle also looks at the CSS and replaces special syntax rules.
<div style='padding:10px;background-color:#f7f7f7;border:1px dashed #123456'><nowiki>[[pix:theme|</nowiki><span style='color:#336699;font-weight:bold;'><nowiki>path/to/image/</nowiki></span><span style='color:#33993A;font-weight:bold;'>imagename</span><nowiki>]]</nowiki></div>
<div style='padding:10px;background-color:#f7f7f7;border:1px dashed #123456'><nowiki>[[pix:theme|</nowiki><span style='color:#336699;font-weight:bold;'><nowiki>path/to/image/</nowiki></span><span style='color:#33993A;font-weight:bold;'>imagename</span><nowiki>]]</nowiki></div>
 
The above pattern is looked for by Moodle and gets replaced with the correct image URL when found.
The above pattern is looked for by Moodle and gets replaced with the correct image URL when found. This is how we are able to add our images into our themes CSS and be sure that they are always correct.
You should note the following things about this pattern when using your own images within CSS:
You should note the following things about this patter when using you own images within CSS:
 
# The bits in black don't change
# The bits in black don't change
# The bit in blue is the path to your image within the pix directory. It shouldn't start with a / but should end with one.
# The bit in blue is the path to your image within the pix directory. It shouldn't start with a / but should end with one.
# The bit in green is the filename, it is just the filename you don't put the extension Moodle works that out.
# The bit in green is the filename ('''no need to include the file extension''')
 
To use gradient.png as the background for our pages, add the following line of CSS to the theme's core.css.
So in our case we want to use background.png as the background of for our pages. To do this we would add the following line of CSS to our themes core.css.
<syntaxhighlight lang="css">
<code css>
body {background-image:url([[pix:theme|gradient]]);}
body {background-image:url([[pix:theme|gradient]]);}
</code>
</syntaxhighlight>
 
Before we look at how to use your own images within your theme's layout files we should first look at how this changes if the image is in a sub directory.
As that's it, however before we look at how to use your own images within your themes layout files we should first look at how this changes if the image is in a sub directory.


Lets assume gradient.png is located here: '''pix/myimages/gradients/gradient.png'''
Lets assume gradient.png is located here: '''pix/myimages/gradients/gradient.png'''


Our CSS would need to be as follows:
The CSS would need to be as follows:
<code css>
<syntaxhighlight lang="css">
body {background-image:url([[pix:theme|myimages/gradients/gradient]]);}
body {background-image:url([[pix:theme|myimages/gradients/gradient]]);}
</code>
</syntaxhighlight>
 
If you want to refer to an image belonging to a plugin, you can do it like this:
===Using your images within you layout files===
<syntaxhighlight lang="css">
The second thing we wanted to do was use logo.jpg within the header of our layout file. This is completely different to using images within CSS as the layout files are PHP not CSS. However I feel this is even easier.
body {background-image:url([[pix:quiz|icon]]);}
 
body {background-image:url([[pix:qtype_ddmarker|grid]]);}
So open up your layout file, for me standard.php and find the correct spot to put your logo. I'm going to insert it as shown below:
</syntaxhighlight>
<code php>
That refers to mod/quiz/pix/icon.png/gif and question/type/ddmarker/pix/grid.png/gif respectively (or whatever icons you have put in your theme to override these).
....
<syntaxhighlight lang="css">
<div id="page-header" class="clearfix">
body {background-image:url([[pix:block_customblock|grid]]);}
    <!-- Logo should go here -->
</syntaxhighlight>
    <h1 class="headermain"><?php echo $PAGE->heading ?></h1>
That refers to blocks/customblock/pix/grid.png/gif/jpg (or whatever icons you have put in your theme to override these).
    <div class="headermenu"><?php
===Using your images within your layout files===
        echo $OUTPUT->login_info();
To use logo.jpg within the a layout php file or layout mustache template you need to add the url for the image to the template context and then include an image tag in the template using the source from the context.
        echo $OUTPUT->lang_menu();
        echo $PAGE->headingmenu;
    ?></div>
</div>
....
</code>
 
To achieve this I simply need to add the following line of code:
<code php>
<?php echo $OUTPUT->pix_url('logo', 'theme'); ?>
</code>
Just like with the CSS example above you should not that I do not need to add the image files extension. Moodle finds it automatically.


Combined the solution is very simple:
Images:
<code php>
<syntaxhighlight lang="php">
....
/pix/logo.jpg
<div id="page-header" class="clearfix">
/pix/myimages/logos/logo.png
    <?php echo $OUTPUT->pix_url('logo', 'theme'); ?>
</syntaxhighlight>
    <h1 class="headermain"><?php echo $PAGE->heading ?></h1>
''theme/yourtheme/layout/somelayout.php''
    <div class="headermenu"><?php
<syntaxhighlight lang="php">
        echo $OUTPUT->login_info();
...
        echo $OUTPUT->lang_menu();
$templatecontext = [
        echo $PAGE->headingmenu;
...
    ?></div>
'imageone' => $OUTPUT->image_url('logo', 'theme'),
</div>
'imagetwo' => $OUTPUT->image_url('myimages/logos/logo', 'theme'),
....
...
</code>
];


The only other thing to look at at this point is how this example would look if logo.jpg was located in a sub folder.
echo $OUTPUT->render_from_template('theme_yourtheme/somelayout', $templatecontext);
So like the previous example lets assume logo.jpg is located here: '''pix/myimages/logos/logo.jpg'''
</syntaxhighlight>
The code for this would be:
''theme/yourtheme/templates/somelayout.mustache''
<code php>
<syntaxhighlight lang="handlebars">
<?php echo $OUTPUT->pix_url('myimages/logos/logo', 'theme'); ?>
...
</code>
<img src="{{{imageone}}}" alt="Please give your image alt text or set the role to presentation" width="50" height="50">
<img src="{{{imagetwo}}}" alt="Please give your image alt text or set the role to presentation" width="50" height="50">
...
</syntaxhighlight>
==Overriding images in your theme==
Overriding images within your theme can be an important part of creating and/or modifying themes.


And thats it! We've now looked at how to use your own images within your theme in both CSS and layout files.
As mentioned, there are three main areas for images within Moodle, core, plugins, and themes. Obviously we won't be overriding theme images as we are in a theme and have full control over that already which just leaves core and plugin images that you may want to override.


==Overriding images in you theme==
Within your theme create the following two directories:
; /pix_core/ : This is where your images to override core images will need to be.
; /pix_plugins/ : This is where images to override plugins will need to be.
Next, copy the images that you wish to override into either the pix_core or pix_plugins directory. You need to replicate the directory structure that the images are located in.


The following two examples illustrate how this works for both core and plugin images.
===Overriding core images===
For this example I am going to override the following two images:
# '''moodle/pix/help.gif''' This image is used for all help image icons and is shown throughout Moodle.
# '''moodle/pix/i/info.gif''' This image is used in several places, most notably the front page if the combo list is being displayed.
So first up help.gif. This is the most basic example of overriding an image. Because the image is directly within Moodle's pix directory we can simply place our new help image into our theme's pix_core directory. There's nothing more to it!


==More information==
The second example if not really any more difficult. Because the image is located within the subdirectory '''i''' we must create a sub directory within our pix_core directory into which we will copy our new help image. That is, the image ends up at '''/pix_core/i/info.png''' inside your theme folder. And again done!


[[Themes 2.0]]
You should also note that, as with any other image in Moodle, the extension doesn't matter. If you want to replace help.gif with a help.png you can just put the png into the correct directory. As long as the filename is the same Moodle will find it.
===Overriding plugin images===
This is a little bit more difficult than overriding core images, but not too much so. For this example, let's say I want to override the following two plugin images:
# '''moodle/mod/forum/icon.gif''' This is the icon that is used everywhere for the forum.
# '''moodle/blocks/customblock/activate.png''' This is an icon in a custom block I have installed.
Just as with core images, we need to put the image in the correct directory structure. In this case it is going to be within our theme's '''pix_plugins''' directory.
The correct path must use the plugin type [[Frankenstyle]] prefix and the plugin name.


[[Themes 2.0 creating your first theme]]
For the examples above the paths to override the plugin images will be:
# '''pix_plugins/mod/forum/'''
# '''pix_plugins/block/customblock/'''
When looking for an overriding image the following path is used.
# theme (static)
# themename => mytheme
# area => pix_plugins for plugin images, pix_core for core images, pix for theme images.
# plugin type => booktool for the book tool sub plugin for example.
# plugin name => print for the name of the sub plugin.
# file name => book for the name of the image.
It then searches for the image with the following extensions gif, png, jpg, jpeg in that order.


[[Plugins]]
So you end up with:
<syntaxhighlight lang="php">
/theme/themename/area/plugintype/pluginname/filename.gif
</syntaxhighlight>
==See also==
* [[Plugins]]
* [http://youtu.be/g_9VM9OMs3Y Adding Images to your Theme] MoodleBites video on YouTube
Using Moodle forum discussions:
* [http://moodle.org/mod/forum/discuss.php?d=151581 Using images within your themes (and the silk icon theme)]
* [http://moodle.org/mod/forum/discuss.php?d=151581#p689883 choosing images based on the users current language]
* [http://moodle.org/mod/forum/discuss.php?d=157935&parent=691903 how to add a category(categorias) img-bullet]

Latest revision as of 20:42, 24 February 2022

A little background

Moodle has a standard way of making use of images within its pages enabling theme designers to take full control over what images are being used and when possible ensures images are passed through Moodle's performance caching system to obtain the best possible performance.

Before we start

  1. Make sure you have a theme and images to work with, one that you have a backup of just in case.
  2. Take note of your theme's main layout file e.g. standard.php.
  3. Take note of your theme's main CSS file e.g. core.css.
  4. Turn on Theme designer mode.

Image locations within Moodle

There are three main areas in which images are located - core, plugin and theme.

Core images are used throughout Moodle and are stored in moodle/pix.

The pix directory contains the following subdirectories:

a - Icons that are not widely used
c - Calendar-related icons
"e" - Editor-related icons
f - File icons for different file types
g - Default user icons and thumbnails
i - General icons
m - Currency symbols
s - Smileys
t - General icons
u - User icons and thumbnails
y - YUI icons

Plugin images are used by plugins and are stored within the plugin's directory e.g. mod/forum/* or blocks/navigation/*

Theme images are used in themes and are stored in the pix subdirectory of a theme.

Adding new images to your theme

To add new images to your theme, such as a background image or a logo, you should start by creating a pix directory within your theme's directory.

Lets assume you have two images, gradient.png and logo.jpg. Copy both files to your theme's pix directory.

The plan is to use gradient.png as the background image for your theme (in CSS) and use logo.jpg in the header (in your theme's layout file).

Using images within CSS

Moodle parses all CSS files. When theme designer mode is off, Moodle combines them into a handful of large CSS files that get cached and served. At the same time Moodle also looks at the CSS and replaces special syntax rules.

[[pix:theme|path/to/image/imagename]]

The above pattern is looked for by Moodle and gets replaced with the correct image URL when found. You should note the following things about this pattern when using your own images within CSS:

  1. The bits in black don't change
  2. The bit in blue is the path to your image within the pix directory. It shouldn't start with a / but should end with one.
  3. The bit in green is the filename (no need to include the file extension)

To use gradient.png as the background for our pages, add the following line of CSS to the theme's core.css.

body {background-image:url([[pix:theme|gradient]]);}

Before we look at how to use your own images within your theme's layout files we should first look at how this changes if the image is in a sub directory.

Lets assume gradient.png is located here: pix/myimages/gradients/gradient.png

The CSS would need to be as follows:

body {background-image:url([[pix:theme|myimages/gradients/gradient]]);}

If you want to refer to an image belonging to a plugin, you can do it like this:

body {background-image:url([[pix:quiz|icon]]);}
body {background-image:url([[pix:qtype_ddmarker|grid]]);}

That refers to mod/quiz/pix/icon.png/gif and question/type/ddmarker/pix/grid.png/gif respectively (or whatever icons you have put in your theme to override these).

body {background-image:url([[pix:block_customblock|grid]]);}

That refers to blocks/customblock/pix/grid.png/gif/jpg (or whatever icons you have put in your theme to override these).

Using your images within your layout files

To use logo.jpg within the a layout php file or layout mustache template you need to add the url for the image to the template context and then include an image tag in the template using the source from the context.

Images:

/pix/logo.jpg
/pix/myimages/logos/logo.png

theme/yourtheme/layout/somelayout.php

...
$templatecontext = [
...
'imageone' => $OUTPUT->image_url('logo', 'theme'),
'imagetwo' => $OUTPUT->image_url('myimages/logos/logo', 'theme'),
...
];

echo $OUTPUT->render_from_template('theme_yourtheme/somelayout', $templatecontext);

theme/yourtheme/templates/somelayout.mustache

...
<img src="{{{imageone}}}" alt="Please give your image alt text or set the role to presentation" width="50" height="50">
<img src="{{{imagetwo}}}" alt="Please give your image alt text or set the role to presentation" width="50" height="50">
...

Overriding images in your theme

Overriding images within your theme can be an important part of creating and/or modifying themes.

As mentioned, there are three main areas for images within Moodle, core, plugins, and themes. Obviously we won't be overriding theme images as we are in a theme and have full control over that already which just leaves core and plugin images that you may want to override.

Within your theme create the following two directories:

/pix_core/
This is where your images to override core images will need to be.
/pix_plugins/
This is where images to override plugins will need to be.

Next, copy the images that you wish to override into either the pix_core or pix_plugins directory. You need to replicate the directory structure that the images are located in.

The following two examples illustrate how this works for both core and plugin images.

Overriding core images

For this example I am going to override the following two images:

  1. moodle/pix/help.gif This image is used for all help image icons and is shown throughout Moodle.
  2. moodle/pix/i/info.gif This image is used in several places, most notably the front page if the combo list is being displayed.

So first up help.gif. This is the most basic example of overriding an image. Because the image is directly within Moodle's pix directory we can simply place our new help image into our theme's pix_core directory. There's nothing more to it!

The second example if not really any more difficult. Because the image is located within the subdirectory i we must create a sub directory within our pix_core directory into which we will copy our new help image. That is, the image ends up at /pix_core/i/info.png inside your theme folder. And again done!

You should also note that, as with any other image in Moodle, the extension doesn't matter. If you want to replace help.gif with a help.png you can just put the png into the correct directory. As long as the filename is the same Moodle will find it.

Overriding plugin images

This is a little bit more difficult than overriding core images, but not too much so. For this example, let's say I want to override the following two plugin images:

  1. moodle/mod/forum/icon.gif This is the icon that is used everywhere for the forum.
  2. moodle/blocks/customblock/activate.png This is an icon in a custom block I have installed.

Just as with core images, we need to put the image in the correct directory structure. In this case it is going to be within our theme's pix_plugins directory. The correct path must use the plugin type Frankenstyle prefix and the plugin name.

For the examples above the paths to override the plugin images will be:

  1. pix_plugins/mod/forum/
  2. pix_plugins/block/customblock/

When looking for an overriding image the following path is used.

  1. theme (static)
  2. themename => mytheme
  3. area => pix_plugins for plugin images, pix_core for core images, pix for theme images.
  4. plugin type => booktool for the book tool sub plugin for example.
  5. plugin name => print for the name of the sub plugin.
  6. file name => book for the name of the image.

It then searches for the image with the following extensions gif, png, jpg, jpeg in that order.

So you end up with:

/theme/themename/area/plugintype/pluginname/filename.gif

See also

Using Moodle forum discussions: