This is the celebrated journal of Mr. Simon Collison A.K.A Colly

Expression Engine Fresh Produce

20th October 2006

We all know that fresh content keeps the visitors coming back, so it’s important that what’s new is clearly highlighted. Of course, for many of the situations you’re going to be tackling with Expression Engine, the typical reverse chronological blog structure handles this perfectly: freshest is top of the pile.

But what happens when you’re showing content in a non-chronological way (like in a portfolio, directory or store catalogue) and you want to visually flag entries added in the last couple of months? Read on for a simple solution that worked for me, and hopefully might work for you, too.

Let’s start with my goals:

  1. Use existing Expression Engine tags where possible and only revert to PHP when essential
  2. Keep it as simple and flexible as possible
  3. Have the option to add a textual or a style-based flag

So, here goes.

First we’ll get down to the nitty-gritty of what we’re trying to achieve, and that seems to be all about comparing today’s date with the date an article was created, and working out if the difference between the two fits within our definition of recent content. At this point I scratched my head a little, and thought about calculating offsets in days, before settling on something much simpler. Here’s the logic:

  • What month is it now?
  • Was the article in question created this month?

Not quite what I’m after though, because articles added near the end of the month don’t have much chance to proudly wave the recent flag. So my second attempt became:

  • What month is it now?
  • What month was it last month?
  • Was the article in question created this month or last month?

Now, achieving this in Expression Engine. We’re going to start by setting up some dynamically assigned global variables, then we’ll compare them to the relevant article using some if:else based fun within our weblog tags.

Somewhere near the top of the template, add the following statement, which combines an Expression Engine tag with a tiny snippet of PHP. You’ll need to make sure PHP is allowed at the output stage for the template you’re using.

{assign_variable:current_month='<? echo date("n"); ?>'}

In English, this simply means “make me a variable called ‘current_month’ that uses PHP to go fetch the value of the current month”. We want our answer to be the number of the current month (10,11,12 etc) without the leading zero (1,2,3 etc) because this will make is easy to do comparisons later. The “n” tells PHP to use that date format.

In the same way, we’ll get the number for last month by using the same code, but with -1 added to it.

{assign_variable:last_month='<? echo date("n")-1; ?>'}

Simple so far. If it’s October, we’ve now got a variable called “current_month” with a value of “10” and a variable called “last_month” with a value of “9”. If you’re new to dynamically assigning variables, the beauty is that next month they’ll automatically become “11” and “10” respectively.

Now we’ve got the variables, we’ll use them within an Expression Engine weblog tag. In this example, we’re showing a single entry from the “work” weblog using the following tag:

{exp:weblog:entries weblog="work" limit="1"}

We’ll show the title of the article using something like:

<h4>{title}</h4>

And now here’s the almost clever bit:

{if {entry_date format="%n"} =="{current_month}"}

This is new content

{if:elseif {entry_date format="%n"} == "{last_month}"}

This is last month's content

{if:else}

This is old content

{/if}

Then finish up with any weblog variables you need to show in the entry, and close the weblog tag using {/exp:weblog:entries}.

We’re using if:else statements which are super-simple but super-powerful. They basically allow us to test for situations and, based on the result, modify the outcome. In Expression Engine they can be used to help accomplish many useful tasks such as showing or hiding content based on whether the user is logged-in or logged out, greeting members by name, or capturing and reacting to the structure of a URL.

In this situation, we’re using them to say:

  • If the month the article was submitted is the same as our “current_month” variable
  • Show the “New content” heading
  • If the month the article was submitted is the same as our “last_month” variable
  • Show the “Last month’s content” heading
  • If neither of those situations apply
  • Show the “Old content” heading

And we’re done. Of course, you can easily apply different styles to really call attention to the new stuff, or show the same message for current and last month. If you really need to specify recency in an exact number of days, there’s a neat solution here, but you I think my version is a little easier to understand, and contains some techniques that could be useful to keep in your EE bat-belt.

Responses

# Owen responded on 21st October 2006 with...

I’m loving these ExpressionEngine tips and techniques. As a recent convert to EE I’ve found that practical examples of what EE can do are very useful. Keep it up and thanks!

# Leslie responded on 21st October 2006 with...

Yes, I’m loving them too. Fire Simon and make Collylogic a full-time EE and web dev blog!

Of course, I might be a bit biased ;)

# Geof Harries responded on 21st October 2006 with...

Between Simon’s and Mark’s site, we EE users have such a wealth of great information at our hands. Here’s hoping that Jambor-EE really takes it to next level.

# Hyun responded on 28th October 2006 with...

I am in the midst of incorporating EE into the new design of my website. And your tip is exactly what I need to implement! Thanks for a highly useful article! :)

Responses are now disabled Your ability to respond is disabled automatically some 30 days after articles are published, or manually much sooner if spamming guttersnipes target a particular article.

Prev 650 Next

Superfluous Aside

Archived in ExpressionEngine, Design & Web

There are 4 responses

External References

Copyright © Mr. Simon Collison 2003-2017. Protected and licensed under a Creative Commons License. Grab the RSS feed

Engineered in Nottingham, scaffolded by ExpressionEngine, steam-pumped by United & kept alive with tea and hugs.