We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32645
    • 377 Posts
    I’ve got a template variable which holds a numeric value, but would like to apply number_format or currency formatting to it - what is the best way to do this?

    Thanks.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 32645
        • 377 Posts
        Hi there.

        Thanks for this -

        I’ve tried this in my Ditto call, however the output is always zero.

        Snippet.
        <?php
        $TVname = 'tv_price';
        
        // fetch the output array of the TV
        $TVOutput = $modx->getTemplateVarOutput($TVname);
        
        return ( number_format($TVOutput,2) );
        ?>
        


        <a href="[~[+parent+]~][+alias+]">[+title+]</a> - £[!numberformat_widget!]
        


        How do I make ditto pass the price parameter to the snippet, and make the snippet read the parameter and act upon it?



          • 11975
          • 2,542 Posts
          Hi,

          you should create a phx modifier instead.

          Create a snippet named phx:numberFormat
          Paste this code:

          return ( number_format($output,2) );
          


          and in your ditto tpl

          [+mytv:numberFormat+]

          :-)
            Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
            • 32645
            • 377 Posts
            That worked perfectly!

            Many thanks
              • 15558
              • 108 Posts
              Hi there,

              Following the (rt)fm, I copied this 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);'));
                  $optionsArray[$params[0]] = $params[1];
              }
               
              $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;


              [[+myTVName:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]




              This kind of works, let's say it works, but it also shows me this error :

              Notice: Undefined offset: 1 in C:\my\path\core\cache\includes\elements\modsnippet\19.include.cache.php on line 14.

              Line 14 is this one in the cached file
                  $optionsArray[$params[0]] = $params[1];


              I'm no PHP developer and I feel sorry to ask you for the solution but such code is almost like russian to me. Can someone help me ?

              Modx 2.2.7 [ed. note: kehezen last edited this post 13 years, 4 months ago.]
                Sorry for that ⇧ , I'm no dev but I wanna try
                • 3749
                • 24,544 Posts
                I think the problem is in the tag. The last two properties may not have proper values.

                Try this:

                [[+myTVName:numberformat=`&decimals=2&dec_point=','&thousands_sep='.'`]]
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 15558
                  • 108 Posts
                  thank you Bob,

                  though this is not the issue, nor the solution.

                  Actually, with this call :
                  [[+myTVName:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]


                  and for a TV value of 374000, the output is 374'000,00 : the call outputs what we expect.

                  (With your suggestion, the output becomes 374'000'00.)

                  The only problem is the display of the above-mentioned error...

                  Has it something to do with revo's newest version 2.2.7 ?
                    Sorry for that ⇧ , I'm no dev but I wanna try
                    • 3749
                    • 24,544 Posts
                    See what happens if you change the code to this:


                    if (isset($params[1])) {
                        $optionsArray[$params[0]] = $params[1];
                    } else {
                        $optionsArray[$params[0]] = '';
                    }
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 15558
                      • 108 Posts
                      that solves the problem !

                      Thank you very much Bob.

                      Here's the whole and correct snippet for whoever needs it :

                      <?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;
                      
                        Sorry for that ⇧ , I'm no dev but I wanna try