Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Number formatting in Volt

I would like to round a number automatically in Volt, so that 1.456 becomes 1.5 and so on. I cannot find the standard function to do this.

Can somebody help me?

edited Mar '14

You can extend volt with your own filter:

<?php         
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$compiler = $volt->getCompiler();         
$compiler->addFilter('roundMe', 'round');

This will add the filter roundMe to volt which you can be used in your template



7.6k
Accepted
answer
edited Jan '16

You can add any function to your tag service and use it in volt easily. I show you how:

<?php
namespace Tartan;

class Tag extends \Phalcon\Tag
{
    static public function noformat($input)
    {
        [...]
        return round($input, 2);
    }
}

then add your Tag service to your di :

$di->set('tag', function() {
    return new \Tartan\Tag();
});

use it like below in your volt file:

{{ tag.noformat(1.456) }}

or you can add your own function/filter to your volt compiler like:

$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$compiler = $volt->getCompiler();
//This binds the function name 'noformat' in Volt to the PHP function 'round'
$compiler->addFunction('noformat', 'round');
// OR
$compiler->addFilter('noformat', 'round');

and use it like below in your volt file:

{{ noformat(1.456) }}

Thanks both of you for your answers.

When I utilize this method, I receive: Macro 'noformat' does not exist

I can use the tag extension in the controller:

$tag = new \Tartan\Tag(); echo $tag->noformat(1.34534);

I can pass it to the view via: $this->view->tag = $tag;

and then use it like {{ tag.noformat(1.34534) }}

But I cannot use it directly as you have specified without receiving the above mentioned error. What am I missing?

Thanks!

You can add any function to your tag service and use it in volt easily. I show you how:

<?php
namespace Tartan;

class Tag extends \Phalcon\Tag
{
   static public function noformat($input)
   {
       [...]
       return round($input, 2);
   }
}

then add your Tag service to your di :

$di->set('tag', function() {
   return new \Tartan\Tag();
});

use it like below in your volt file:

{{ noformat(1.456) }}
edited Jan '16

try {{ tag.noformat(1.456) }} for Phalcon version 2 as you mentioned. The answer was for version 1 :)

When I utilize this method, I receive: Macro 'noformat' does not exist

I can use the tag extension in the controller:

$tag = new \Tartan\Tag(); echo $tag->noformat(1.34534);

I can pass it to the view via: $this->view->tag = $tag;

and then use it like {{ tag.noformat(1.34534) }}

But I cannot use it directly as you have specified without receiving the above mentioned error. What am I missing?

Thanks!

You can add any function to your tag service and use it in volt easily. I show you how:

<?php
namespace Tartan;

class Tag extends \Phalcon\Tag
{
   static public function noformat($input)
   {
       [...]
       return round($input, 2);
   }
}

then add your Tag service to your di :

$di->set('tag', function() {
   return new \Tartan\Tag();
});

use it like below in your volt file:

{{ noformat(1.456) }}