A quick look at some helpful MODX output modifiers

Sat Jul 16, 2022 Share on:

MODX Output modifiers bring near limitless possibilities to your code by allowing you to execute PHP code on a value. Here are some of my favourite one-liners and useful tips on using output modifiers.

<!-- Output the current year by setting the placeholder year to a value of now: 2022 -->
[[!+year:default=`now`:strtotime:date=`%Y`]]

<!-- Outputs the current MODX users full name -->
[[!+modx.user.id:userinfo=`fullname`]]

<!-- Simple if else statement -->
[[*pagetitle:eq=`Home`:then=`You are on the Home page`:else=`You are note on the Home page`]]

<!-- Generate a thumbnail using PHPThumb -->
[[*image:phpthumbof=`w=120&h=120`]]

How do we build our own output modifiers?

MODX output modifiers are basically snippets that take the value of the placeholder and perform operations on it. To make your own, create a new snippet and grab the value of the placeholder using $input.

Below we'll create an output modifier that takes a comma-separated list of numbers and returns them in descending order, the snippet is called orderdesc and you can use the modifier like so: [[*numbers:orderdesc]]

If we have a TV called numbers with a value of 5, 8, 2, 9, 1, 7, 3, it will return 9, 8, 7, 5, 3, 2, 1

<?php

// Explode , in string to array
$arr = explode(',', $input);
// Sort array from High to low 
arsort($arr);
// turn back into a string 
return implode(', ', $arr);

Next