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

strtotime function in volt

When trying to get strtotime to work in volt i get this error:

Macro 'strtotime' does not exist

Service.php

$di->set('view', function () { $config = $this->getConfig();

  $view = new View();

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

  $view->registerEngines([
      '.volt' => function ($view) {
          $config = $this->getConfig();

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

          $volt->setOptions([
              'compiledPath' => $config->application->cacheDir . 'volt/',
              'compiledSeparator' => '_'
          ]);

          $compiler = $volt->getCompiler();
          $compiler->addFunction('strtotime', 'strtotime');

          return $volt;
      }
  ]);

  return $view;

}, true);

edited Feb '18

How do you use it? Functions should be used like so: {{ functionName(variable) }}.

If you want to use it as a modifierd: {{ variable|functionName }} add it like so: $compiler->addFilter('strtotime', 'strtotime');

Adding filter instead dosent work.

I use it as {{ functionName(variable) }} and the filter as {{ variable|functionName }}

What am I doing wrong? It's like the filter or funtion is never being added



8.3k
edited Feb '18

Please check your compiled view, it seems look like:

<p><?= strtotime('+1 day')  //exists ;?></p>
<p><?= $this->callMacro('get_class', []) //not exist ;?></p>

there is somewhere in cache/volt folder. Try to use another function and check whether "callMacro" appears. If not, check your configuration, in DEV should be:

volt:
    compileAlways: true

Nothing of the above worked. Only the <p><?= strtotime('+1 day') //exists ;?></p> printed out strtotime.

CallMacro says the same.

Is this a bug with Phalcon?

// Service definition
$compiler->addFunction('strtotime', 'strtotime');

// Volt code
{{ strtotime('+1 DAY') }} 
// 1518454697 

Working as intended!

Macro 'strtotime' does not exist

How is min not working then?