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.

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?



82.2k
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 . ');';
  });


6.3k

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!



20.8k

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?



20.8k

@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:

```php 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:

```php $compiler = $volt->getCompiler();

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