lightncandy icon indicating copy to clipboard operation
lightncandy copied to clipboard

Arrow function helpers generate invalid php code

Open thewunder opened this issue 2 years ago • 1 comments

The PHP Code:

require('./vendor/autoload.php');
use LightnCandy\LightnCandy;

// template doesn't matter
// Helpers:
$helpers = array(
  'formatInt' => fn($value) => number_format($value)
);

$phpStr = LightnCandy::compile($template, array(
  // Used compile flags
  'flags' => LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_HANDLEBARS,
  'helpers' => $helpers,
));
// Generated php code
use \LightnCandy\SafeString as SafeString;use \LightnCandy\Runtime as LR;return function ($in = null, $options = null) {
    $helpers = array(            'formatInt' =>             $hb->addHelper('formatInt', fn($value) => number_format($value));
    ,
);
/// etc...

Results: ParseError: syntax error, unexpected token ";", expecting ")" on line 2

The Issue:

Arrow functions as helpers generate invalid php code Simply changing to a full closure fixes the issue

thewunder avatar Nov 02 '23 20:11 thewunder

Arrow function helpers are now supported in https://github.com/devtheorem/php-handlebars.

theodorejb avatar Mar 19 '25 22:03 theodorejb