zordius / lightncandy

An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ),
https://zordius.github.io/HandlebarsCookbook/
MIT License
608 stars 76 forks source link

Custom each does not support `as | a index |` syntax #371

Open willdkp opened 3 months ago

willdkp commented 3 months ago

The PHP Code:

 $helpers = array(
'myeach' => function ($context, $options) {
    $ret = '';

    $theArray = json_decode($context, true);

    if(!is_array($theArray)) {
        return '';
    }

    foreach ($theArray as $i) {
        $ret .= $options['fn']($i);
    }
    return $ret;
},
);

$options = array(  
    'flags' => LightnCandy::FLAG_ERROR_LOG | LightnCandy::FLAG_STANDALONEPHP | LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_PARENT | LightnCandy::FLAG_JSTRUE | LightnCandy::FLAG_JSOBJECT,  
    'helpers' => $helpers
);  

$php = LightnCandy::compile($template, $options);  
$renderer = LightnCandy::prepare($php);  

$handlebarsOutput = $renderer($config);  

Template:

{{#myeach '[{"a":"ayy", "b":"bee"},{"a":"zzz", "b":"ccc"}]' as | newContext index | }} 
Foo {{newContext.a}} {{index}}
 {{/myeach}}

The Issue:

As in the example I have a custom each helper. When I attempt to use the as | value index| syntax as described in handlebars documentation it does not work. this will return the object that was pulled from the JSON okay, but newContext and index print nothing. Such that the rendered output is simply Foo Foo

Do I need additional flags to enable this? What am I doing wrong here?