Skip to content

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

@willdkp

Description

@willdkp

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions