占位符的格式化输出

问题描述 投票:0回答:1

我正在创建一个动态列表

placeholder
,这些占位符中保存的一些值是十进制数字,应该代表金钱。

我想知道是否有办法可以将它们格式化为这样显示?

类似

[[+MoneyField:formatmoney]]

我看到http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers)但我没有在这里查看一种方法。

php string-formatting modx modx-revolution modx-chunks
1个回答
3
投票

您绝对可以,在您发布的链接上的“创建自定义输出修饰符”标题下,描述了如何将代码段名称放置为输出修饰符。此代码片段将在名为

[[+MoneyField]]
的变量中接收
$input
值。

因此,您必须创建这个自定义片段,它可以像

一样简单
return '$'.number_format($input);

执行此操作的另一个版本是直接调用代码片段,而不是像这样作为输出修饰符:

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

我不确定在这种情况下两者之间是否有任何区别。显然,当将其作为片段而不是输出修饰符调用时,您可以将任何值传递到数字格式片段中。我确信两者之间存在一微秒的性能差异,但我担心我不知道哪一个会获胜。 ;)

更新: 实际上在此链接上找到了您想要实现的确切示例; http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-例子

片段:

<?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;

用作输出修饰符:

[[+price:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]
© www.soinside.com 2019 - 2024. All rights reserved.