【问题标题】:Format Output of Placeholder占位符的格式输出
【发布时间】:2013-09-06 13:03:35
【问题描述】:

我正在创建一个placeholders 的动态列表,这些占位符中的一些值是十进制数字,应该代表金钱。

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

类似[[+MoneyField:formatmoney]]

我看到http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers),但在这里我看不到这样做的方法。

【问题讨论】:

    标签: format string-formatting modx modx-revolution modx-chunks


    【解决方案1】:

    您绝对可以,在您发布的链接上的“创建自定义输出修饰符”标题下,描述了如何将 sn-p 名称作为输出修饰符。此 sn-p 将在名为 $input 的变量中接收 [[+MoneyField]] 值。

    所以你必须创建这个自定义的 sn-p,它可以像

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

    这样做的另一个版本是直接调用 sn-p 而不是像这样作为输出修饰符:

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

    我不确定在这种情况下两者之间是否有任何区别。显然,当您将其作为 sn-p 而不是输出修饰符调用时,您可以将任何值传递给数字格式 sn-p。而且我敢肯定两者之间存在微秒的性能差异,但恐怕我不知道哪一个会赢。 ;)

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

    片段:

    <?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=.`]]
    

    【讨论】:

    • 不错的克里斯蒂安,几分钟后试试
    猜你喜欢
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多