A theme is a collection of resource files that gives a site its visual
identity through the use of colors and typography. A theme consists of
cascading style sheets (CSS), JavaScript files and an imaging
configuration that tells the
Imaging module what to do
with images for a particular page or component.
What is a theme?
A theme is a module or part of a module composed of CSS, JS and images.
When to use a theme
The Site module provides
theme functionality. You can only create a theme if you use this module
and have configured a site definition.
Themes and modules
Theme resources can be stored in light (YAML) or Maven
modules. Themes can reside in a dedicated theme module or
form part of another module. A special module version handler is not
necessary.
Resource loading
All Magnolia resources, including theme
resources, are loaded under the /.resources path.
In a Maven module, the resources are located in
/src/main/resources/<moduleName> and any subfolder.
In a light module, resources can be located in any subfolder of your
module (for example, /<moduleName>/webresources/).
Theme resources can also be loaded from an external source by specifying
the full URL. This is very useful if you use a
CDN to store,
cache and deliver your static resources.
Themes are an easy way to work with resources without having to modify
the head section of your HTML.
You can define more than one theme and switch between them or apply a
different theme to a variation of your site. For example, you could use
a different theme for pages targeted at U.S. users and European users
or a seasonal theme for different times of the year.
You can still use different or additional overriding resources on a
page-template level.
Configuring a theme
Where to configure a theme
Themes can be configured in:
Any module.
YAML on the file system or in the JCR.
Proper location
The configuration must reside in the themes folder of the module in
/<module-name>/themes/<theme-name>. This can be done in YAML or JCR.
Old location
The old and deprecated location for themes is
site/config/<theme-name>. Themes in this location could only be
configured in the JCR. Themes configured in the old location should be
moved to the new location.
Example
travel-demo-theme in travel-demo/themes/travel-demo-theme.yaml
Embeds a file content fingerprint in the resource URL. The fingerprint
consists of a timestamp in the yyyy-MM-dd-HH-mm-ss-SSS format and the
word "cache", both preceded by the standard selector (~).
For example, the travels-magnolia link above becomes
/.resources/travel-demo-theme/css/travels-magnolia~2015-08-16-09-49-00-000~cache.css.
Google recommends
fingerprinting as a best practice.
Media associated with the linked CSS file. If you use a responsive web
design framework like Bootstrap or
Foundation, set this property to all. See
Media descriptors
for a list of available media.
conditionalComment
optional
Defines a conditional comment
for the linked CSS file. Conditional comments are used to
overcome the limitations of legacy browsers such as Internet
Explorer 9 and earlier. For example, the conditional comment
"lte IE9" would be translated to:
<!--[if lte IE9]>
//inclusion of the stylesheet or javascript resource
<![endif]-->Copy
This simple theme example uses only the default Bootstrap-minimized CSS
with no customizations.
JavaScript files
Links to JavaScripts are configured in the jsFiles node of the theme.
JavaScript is responsible for client-side functionality such as carousel
paging, AJAX, menu animations, trees, embedded video player, mouse wheel
support, Google Analytics and much more.
Example: jsFiles configuration in travel-demo-theme in
travel-demo/themes/travel-demo-theme.yaml.
Arbitrary node name. Add a node for each JavaScript file.
addFingerPrint
optional
Embeds a file content fingerprint in the resource URL. The fingerprint
consists of a timestamp in the yyyy-MM-dd-HH-mm-ss-SSS format, and the
word "cache", both preceded by the standard selector (~).
For example, the modernizr link above becomes
/.resources/travel-demo-theme/js/modernizr-2.6.2.min~2015-08-16-09-49-00-000~cache.css.
Google recommends
fingerprinting as a best practice.
Here is an example JavaScript alert box using JQuery and the default
Bootstrap-minimized CSS.
Image variations
Themes can make use of the
Imaging module to
preconfigure image variations. A variation is a specific configuration
that defines features of a target image. Variations can range from
complex operations, such as applying filters and overlays, to simple
resizing of the target image.
Variations are an effective alternative to resizing images with CSS:
Image size is reduced and less data transferred.
Editors can upload images of any size, which will automatically be
resized to fit the template.
Image sizes are uniform throughout the site.
Variations are configured in the imaging node of the theme.
Example: imaging configuration in travel-demo-theme in
travel-demo/themes/travel-demo-theme.yaml.
info.magnolia.templating.imaging.VariationAwareImagingSupport
provides support for image variations.
variations
required
Map of image variations.
<variation name>
required
Name of the variation (rendition). Choose a name that
describes the variation. The name is used in template scripts to
reference the variation. Add one node for every variation.
class
required
You can use one of the available classes or create a custom one. The
available properties depend on the class used for the variation. Any
custom class must implement info.magnolia.imaging.variation.Variation.
info.magnolia.templating.imaging.variation.SimpleResizeVariation
to resize images to a defined size in pixels (see below for available
properties).
info.magnolia.templating.imaging.variation.ImageOperationProvidingVariation
to create your own image operation chain using the mte
image generator.
<properties>
Add properties that are supported by the class used.
enabled
optional
Enables and disables support for image variations.
Configuring a simple resize
The info.magnolia.templating.imaging.variation.SimpleResizeVariation class is suitable for most resize variations.
Properties of SimpleResizeVariation:
Property
Description
<variation name>
class
required
Class used to resize images to a defined size in pixels. Must implement
info.magnolia.templating.imaging.variation.SimpleResizeVariation.
crop
optional, default is true
Defines whether the image can be cropped to fit the desired aspect ratio.
Example: A source image in the DAM is 800x800 px (square). For a
variation, you define the desired target size as 200x100 (rectangle).
The image is first resized to 200x200 and then cropped equally
from the top and bottom to reach 200x100 so that it is not
distorted. If you set crop to false, the image will be
distorted to achieve the desired target size.
expand
optional, default is false
Defines whether the image can be expanded when it is not cropped.
height
optional
Integer to define the height in pixels. You must define either height or width.
width
optional
Integer to define the width in pixels. You must define either height or width.
If you do not define either height or width, the renderer
will internally throw ImagingException and the image will appear in its
original uploaded size.
If you define both height and width, the image will be sized down to the
lower of the two and the rest will be cropped if necessary. Cropping starts
from the middle, so the image is cropped either on the top and bottom or
the left and right.
If you define only height or width, the other property will be calculated
using the aspect ratio of the original image.
Theme resources are typically added to the <head> element of a page
template.
Retrieve the configured theme configured over the
sitefn
function class:
[#assign site =
sitefn.site()!]
[#assign theme =
sitefn.theme(site)!]
Copy
Iterate over the theme configuration and include the configured
resources:
[#list theme.cssFiles as cssFile]
<link rel="stylesheet" href="${cssFile.link}" media="${cssFile.media}" />
[/#list]
[#list theme.jsFiles as jsFile]
<script src="${jsFile.link}"></script>
[/#list]
Copy
Viewing and editing themes
Theme definitions from all sources and origins are available in the
Definitions app. Select
themes in Type to find all theme definitions. You can hotfix themes
in the Resource Files app.
Select the theme definition and click Show in Resources app in the
Definitions app to access the file.
Example: travel-demo-theme in the Definitions app and Resource Files app.