In the previous article, we looked how to empty a TV for resources under selected parent folders. I this, and the following articles, we'll see how to get the current year in code and perform manage content based on it.
What's the Current Year?
This couldn't be much simpler. PHP's
date() function, when called with
"Y" as the only argument will return the current year.
$year = date("Y");
The simplest use of this code is to display the current year on a page. For example, you might have a Copyright notice in your template like this one:
Copyright 2019, Bob Ray
It's a pain to have to start each year by editing the copyright notice in all your templates. It's even worse if the notice in the page content of all your pages (or just some of them). Luckily, it's easy to automate this with a simple snippet.
In your copyright notice, you can just do something like this:
Copyright [[CurrentYear]], Bob Ray
Then, create a snippet called CurrentYear, with this single line of code:
Note that with the version above, the year won't change until the cache is cleared. If you want it to always be current, you can do this:
Copyright [[!CurrentYear]], Bob Ray
Adding the exclamation point calls the snippet uncached so it will always be fresh. It's a little slower, but the snippet is extremely fast, so it's probably just a few milliseconds. If you care about that, you can call it cached and just remember to clear the site cache on the first day of the year.
In the next article, we'll look at a way to control page content based on the current year.
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)