MODx-Feature: Snippets – all rounders

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

Date snippet PHP sourcecode 1.0

<?php
$format="d.m.Y";
$date=date($format);
echo($date);
?>

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

MODx API

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:

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.

Caching

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.

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

JX3

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 :)