Skip to content

Creating a custom modifier

Any snippet can be used as an output modifier. Just specify the snippet name instead of the modifier. For example, we will create a snippet [[makeExciting]] that appends a given number of exclamation marks to the output:

modx
[[*pagetitle:makeExciting=`4`]]

This tag call passes the following parameters to the makeExciting snippet:

ParameterValueExampleDescription
inputElement value$input = `[[*pagetitle]]`;Value of the element the modifier is applied to.
optionsModifier value$options = `4`;Extra parameters (after =).
tokenElement type$token = `*`;Character that defines the tag type.
nameElement name$name = `pagetitle`;Name of the placeholder the modifier is applied to.
tagFull tag$tag = `[[*pagetitle:makeExciting=`4`]]`;The complete tag.

Here is the makeExciting snippet code:

php
<?php
$defaultExcitementLevel = 1;
$result = $input;

if (isset($options)) {
  $numberOfExclamations = $options;
}
else {
  $numberOfExclamations = $defaultExcitementLevel;
}

for ($i = $numberOfExclamations; $i > 0; $i--) {
  $result = $result . '!';
}

return $result;

The tag outputs whatever the snippet returns. In our case it will return the value of the tag [[*pagetitle]] with four exclamation marks.

Подсказка

If the snippet returns an empty string, the original tag value will be output on the page.