MODX Snippet Development I

This is the first in a series of articles about snippet design and impMODX logorovement. In the series, I’d like to walk you through the process of creation and enhancement of a simple snippet. The articles are mainly aimed at people first getting their feet wet with PHP, but even if you’re not a beginner, you might find something of value here.

When I create a utility extra, it usually starts out as a very modest little snippet that meets my exact needs. Later, when I start thinking about how other people might use the snippet, I add features that make the snippet more powerful, more flexible, more efficient, and more user-friendly. I thought it might be interesting to look at the steps in that process with an example snippet.

A while back, a MODX Forum user asked about a way to highlight new resources. If a resource was published within the last two weeks, the user wanted to display the word NEW! next to the pagetitle. In this first article we’ll look at a simple version of the snippet that does just that, then in future articles, we’ll look at progressive enhancements to the snippet with some detours into MODX coding standards and good design. The snippet tag can go in the page template or in the page content.




The Problem

If you want to display something to highlight newly published resources, it’s a real pain to have to edit the resources every day to keep the display up to date. You could use a TV, but then you’d have to update the TV values, which might be even more of a pain. Testing the age of a resource based on its publishedon field is very easy to do in a snippet and it’s a small step from there to returning the appropriate display. We’ll call our snippet NewResource.


The Code

Here’s a version of the HTML that might contain our snippet call:

[code language=”html”]
<h3>[[*pagetitle]] [[!NewResource]]</h3>

The [[!NewResource]] tag will be replaced by the return value of our snippet. Here’s the code of the snippet:

[code language=”php”]
if ( ((strtotime($modx->resource->get(‘publishedon’))) + 1209600) > time()) {
if ($modx->resource->get(‘published’)) {
return ‘<span class="new_resource">NEW!</span>’;
} else {
return ”;



As it is, the snippet does the job. It simply tests the age of the resource and returns the tag containing “NEW!” if it’s less than two-weeks old.

There are lots of ways in which this snippet could be improved. It has some design problems and it’s not very flexible, readable, or user-friendly. The most obvious variation would be to use a different time period. The big number in our code (1209600) is the number of seconds in our time interval (two weeks, in this case). To change the interval, we can just change the number. Google will quickly give you the value to use if you search for something like “How may seconds in one week”. As we’ll see later, there’s a more user-friendly way to do this.

You might also want to change the value returned by the snippet. Remember that the tag will be replaced by whatever you return. It could be a word, some HTML, an image tag, or a class name. At this point, you can just put the tag where you want the return value to appear, and change the value of the string in the return line, but again, that’s not very user-friendly.

In upcoming articles, we’ll look at the process of improving this simple snippet in various ways. Although our snippet has a particular purpose, the steps used to improve it are common to many different types of snippets.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than <a
href=””>Arvixe Web Hosting!

Tags: , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *