We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to write a Volt filter

I want to pretty print a number. This number comes from the database as application.students.matrikel.

The number has 7 or 8 digits and it has to be displayed in this way: 12-345-678. If it has only 7 digits, a zero must be prepended (01-234-567).

I added a new filter to Volt:

  $compiler = $volt->getCompiler();

  // format number
  $compiler->addFilter('pretty', function($resolvedArgs, $exprArgs) 
  {
       $padded = sprintf('%08s', $resolvedArgs);
       $formatted = substr($padded, 0, 2).':'.substr($padded, 2,3).':'.substr($padded, 5,3);
       return $formatted;
  });

In the view, I use

{{ application.students.matrikel|pretty }}

but this gives me always a parse error.

What am I missing?



98.9k
Accepted
answer

Volt filters must return PHP code rather than execute PHP code in runtime, you must do:

class Helpers
{

   public static function pretty($argument)
   {
        $padded = sprintf('%08s', $argument);
        $formatted = substr($padded, 0, 2) . ':' . substr($padded, 2, 3) . ':' . substr($padded, 5, 3);
        return $formatted;
   }

}

And register the filter:

$compiler = $volt->getCompiler();

  // format number
  $compiler->addFilter('pretty', function($resolvedArgs, $exprArgs) 
  {
         return  'Helpers::pretty(' . $resolvedArgs . ');';
  });


10.0k

Wow, this works.

Just for the record: I put the Helpers class in app/library which is a registered directory in the autoloader.

Thanks for the quick answer!



47.7k

I get a segmentation fault here:

$di->set('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));

            // format number
            $volt->getCompiler()->addFilter('pretty', function($resolvedArgs, $exprArgs) 
            {
                   return  'Helpers::pretty(' . $resolvedArgs . ');';
            });

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

This however works in the volt view so the class is loaded:

<p><?php echo Helpers::pretty($ugly->text) ?> ...</p>

Any ideas?



47.7k

@renski Your horse came in and now feel I'm recovering after walking into a lampost.

I disabled xdebug in my php.ini and found it works like a charm.

Thank you.

Where I should place register filter code ?

Volt filters must return PHP code rather than execute PHP code in runtime, you must do:

class Helpers
{

  public static function pretty($argument)
  {
       $padded = sprintf('%08s', $argument);
       $formatted = substr($padded, 0, 2) . ':' . substr($padded, 2, 3) . ':' . substr($padded, 5, 3);
       return $formatted;
  }

}

And register the filter:

$compiler = $volt->getCompiler();

 // format number
 $compiler->addFilter('pretty', function($resolvedArgs, $exprArgs) 
 {
        return  'Helpers::pretty(' . $resolvedArgs . ');';
 });