Deutsche Version subscribe RSS feed


MODx-Feature: Snippets – all rounders

Tags: MODx, Snippets | last edited: 16.06.2010

In MODx, dynamic functions are implemented with PHP based snippets, which are called up in templates, documents or chunks. Snippets are a major component of MODx and enable the dynamic set-up of contents in the first place.

The most prominent snippets are probably Ditto and Wayfinder, which are integral parts of each MODx installation. Ditto is a powerful tool to put out document lists in any form; in this blog, for example, the list “last article” on the right hand side or the content list on the home page. Wayfinder, however, is used for the set-up of website navigations.

Let’s have a look at a simple snippet from a practical kind of view. First of all, we set up the snippet under resources > manage resources > snippets

Create a snippet in MODx

Date snippet PHP sourcecode 1.0


The call for the snippet can now, as mentioned before, be in a template, a document or a chunk: Current date: [[Date]] gives you the result of Current date: 21.04.2008. Our simple date snippet is complete and ready to use!

Snippet parameter

Parameter can be assigned to snippets. If we upgrade our date snippet with the optional parameter format the call for the snippet looks as follows [[Date? &format=`Y`]]. In PHP, the parameter format is now available as a variable and can therefore be used accordingly:

Date snippet PHP sourcecode 1.1

   if(!isset($format)) $format="d.m.Y";         


If snippets hadn’t the MODx API they would only be half as useful, as they allow the developer to access MODx elements in an easy manner. The snippet DocList will be used an example here as it compiles and puts out a document list like Ditto. To begin with, we configure the wanted output in a chunk by using HTML and MODx placeholders:

Chunk DocListTpl

      <a href="[+link+]">more …</a>

This is how a call for DocList and DocList sourcecode could look like. They ensure that all direct child documents of the document with the ID 5 are put out as defined in the chunk DocListTpl:

Call for DocList

<h1>DocList in action:</h1>
   [[DocList? &parentID=`5`]]

DocList snippet PHP sourcecode

   // Compile document array
   // Go through all documents
   foreach($documentArray as $document)
      // Define chunk parameter 
      // Carry out chunk DocListTpl
      $documentOutput.=$modx->parseChunk('DocListTpl', $params, '[+', '+]');
   // Output

As the individual functions of MODx API are described in the MODx documentation I am not going into more detail here. MODx developers who are familiar with SQL should definitely have a look at the MODx DBAPI.


Is a snippet called up in a document with activated caching, the snippet output is being saved in the cache and the snippet won’t be executed when called up in the future. If you want to force the execution of a snippet even when the caching is activated, the call is noted with square brackets + exclamation mark: [!Date? &format=`H:i:s`!]. Consequently, you can ensure that, for example, the time that is being put out by using our date snippet is the current one and is not retrieved from the cache.

Endless opportunities (again!)

Building upon the MODx framework and thanks to API and DBAPI, snippets provide web developers with endless opportunities to develop individual functions for a website or entire web applications. Depending on requirements, data structures can be either based on MODx documents extended with template variables or on individual tables in the MODx database.

Bookmark or recomend this page

Write a comment

Comments are not active anymore


Programmer Niagara visit website

17.08.2010 01:28:51

Great writing and probably one of the best looking MODx pages in existance.

Bogdan visit website

16.06.2010 09:00:53

Hello Alan,

thanks for the hint, you are right. I just removed the ‘$’ character.


Alan Pachuau visit website

16.06.2010 03:07:13

Thanks for the tutorial. But i think you need to remove ‘$’ before ‘intval’. It returns an error.


10.06.2010 17:31:11

Thanks! I tried to implement same kid of functionality as the Ditto call has, but I guess this works also. You can’t believe how damn hard it is to find a solution for this simple problem! I guess it would have been easy to do with PHx, but I can’t download it from the MODx repository and well, where else could I download it? :P

You have an awesome looking MODx site here. One of the best I have ever seen actually. It actually looks like a blog unlike the "Mini Blog" which comes with the default install of MODx.

But again, thank you so much! Spent hours finding even some kind of a solution :)


08.06.2008 15:36:10

Very nice explanation :)