Season Snippet

Have your website automatically change with the seasons.

By Bob Ray
February 15, 2022
Season Snippet

A long time ago in MODX Evolution, I had a client who wanted to show photos related to the current season and offer four different seasonal coupons. I created a Snippet to do the job, and I thought I’d provide an updated MODX Revolution version of the Snippet for people who want to do something similar.

This Snippet allows you to automatically produce seasonal changes on your website. It can be used to simply display the name of the current season or to pull in Chunks that “seasonalize” the site in various ways. It can be used multiple times on the same page. If you’re using the Chunk version, you can create four Chunks called summer, fall, winter, and spring (these are the default Chunk names). Optionally, you can send the names of the four Chunks as properties.

Usage

This tag will return just the name of the season:

[[!Season? &nameOnly=`1`]]

This one will return the content of the appropriate Chunk if the Chunks are called summer, fall, winter, and spring:

[[!Season]]

This tag sets the names of the Chunks to use for each season:

[[!Season?
    &summerChunk=`MySummerChunk`
    &fallChunk=`MyFallChunk`
    &winterChunk=`MyWinterChunk`
    &springChunk=`MySpringChunk`
]]

The Snippet

Paste this code into a Snippet called Season and save it.

<?php
/* Season snippet */

if (! function_exists('season')) {
    function season() {
        $limits = array(
            '/12/21' => 'winter',
            '/09/21' => 'fall',
            '/06/21' => 'summer',
            '/03/21' => 'spring',
            '/01/01' => 'winter'
        );
        $adate = date("M d Y"); /* set $adate to today */

        foreach ($limits AS $key => $value) {
            $limit = date("Y") . $key;
            if (strtotime($adate) >= strtotime($limit)) {
                return $value;
            }
        }
    }
}

$summerChunk = $modx->getOption('summerChunk', $scriptProperties, 'summer');
$fallChunk = $modx->getOption('fallChunk', $scriptProperties, 'fall');
$winterChunk = $modx->getOption('winterChunk', $scriptProperties, 'winter');
$springChunk = $modx->getOption('springChunk', $scriptProperties, 'spring');

$nameOnly = $modx->getOption('nameOnly', $scriptProperties, false);

/* Get the name of the current season */
$season = season();

/* Figure out what to return */
if (!empty($nameOnly)) {
    /* Return just the season name */
    $output = $season;
} else {
    /* Return the content of the appropriate chunk */
    $output = $modx->getChunk(${$season . 'Chunk'});
}
return $output;

Holidays

Instead of the four seasons, you might want to automatically present content related to various holidays. All you need to do is modify the dates and Chunk names. That will work if you use a generic time span for each holiday. If you need specific content for some individual holidays like Easter or Memorial Day, that don’t always occur on the same date, you’d need to integrate some holiday-finding procedures like the code here or here.

Style Changes

It’s probably occurred to you that you might want to change the CSS of pages at your site for either seasons or holidays.

You could use this Snippet to change the CSS with the seasons by putting the path to a different CSS file in each Chunk and using the Snippet call in your Template where the CSS file is specified. A simpler method would be to name the CSS files summer.css, fall.css, winter.css, and spring.css, and putting a tag like this in the head section of your Template:

<link rel="stylesheet" href="https://assets/css/[[!Season? &nameOnly=`1`]].css" />

You could do something similar for an image reference, so that the Snippet could be used to change an image or background image with the seasons.

You could also make a slight modification of the Snippet to have it return the name of a CSS file that’s customized for the current season or holiday. Once you do that, you can just replace the name of the CSS file in your template with the Snippet tag. Alternatively, you could add a new line below the line that loads the CSS file to load a second CSS file that would override some of the CSS.

Internationalization

If your site is in a different language than English, you can change the season names wherever they appear in the code, and if you’re using Chunks, change the names of your Chunks accordingly or just send their names in the Snippet properties. Be sure to change all of them and preserve the case of each season. You don’t need to change the date formatting, since the date is never displayed and only digits are used for the calculation.

The date ranges in the Snippet above are in the form /month/day and correspond to the official seasons in the USA. You may wish to change them for other locales.

If you want to use the Snippet on a multi-language site, and plan to display the season name, you can create lexicon files. You’ll need to send an &language property in the Snippet tag with the two-letter language code and add these lines just below the function to load the appropriate lexicon file:

$language = $modx->getOption('language', $scriptProperties, 'en');
$modx->setOption('cultureKey', $language);
$modx->lexicon->load('season:default');

Change the line that returns just the season name to:

$output = $modx->lexicon($season);

Then, in the lexicon file (say at, core/components/season/lexicon/fr/default.inc.php), do something like this:

$_lang['spring'] = 'printemps';
$_lang['winter'] = 'hiver';
/* etc. */

A simpler method would be to just name your Chunks with a season prefix, like enspring, frspring, despring, etc. Then modify the Snippet to get the current two-letter language code with $lang = $modx->getOption('cultureKey'), and then prepend that language code to the $season variable at the end of the Snippet, like this:

return $lang . $season;

Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.