Question

I am creating a dynamic list of placeholders, some of the values held in these place holders are decimal numbers that are supposed to represent money.

What I'm wondering is if there is a way I can format them to display as such?

Something like [[+MoneyField:formatmoney]]

I see http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers) but I do not see a way to do this here.

Was it helpful?

Solution

You most definitely can, under the header "Creating a Custom Output Modifier" on the link you posted it's described how you can place a snippet name as a output modifier. This snippet will recieve the [[+MoneyField]] value in a variable called $input.

So you'd have to create this custom snippet which could be as simple as

return '$'.number_format($input);

Another version of doing this is calling the snippet directly instead of as an output modifier like so:

[[your_custom_money_format_snippet ? input=`[[+MoneyField]]`]]

I'm not sure if theres any difference between the two in this case. Obviously you can pass any value into the number format snippet when calling it as a snippet instead of an output modifier. And i'm sure theres a microsecond of performance difference in the two but i'm afraid i don't know which one would win. ;)

Update: Actually found the exact example you want to implement on this link; http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-examples

Snippet:

<?php
$number = floatval($input);
$optionsXpld = @explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
    $params = @explode('=', $xpld);
    array_walk($params, create_function('&$v', '$v = trim($v);'));
    if (isset($params[1])) {
        $optionsArray[$params[0]] = $params[1];
    } else {
        $optionsArray[$params[0]] = '';
    }
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;

Used as output modifier:

[[+price:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top