Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
willdkp opened this issue Mar 22, 2024 · 1 comment
Open

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

willdkp opened this issue Mar 22, 2024 · 1 comment

Comments

@willdkp
Copy link

willdkp commented Mar 22, 2024

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?

theodorejb added a commit to devtheorem/php-handlebars that referenced this issue Mar 31, 2025
@theodorejb
Copy link

In order for this to work the block parameters have to be explicitly passed to the fn() block renderer. This can be done with PHP Handlebars as follows:

use DevTheorem\Handlebars\{Handlebars, HelperOptions, Options};

$myEach = function ($context, HelperOptions $options) {
    $theArray = json_decode($context, true);
    if (!is_array($theArray)) {
        return '';
    }

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

$template = <<<tpl
    {{#myeach '[{"a":"ayy", "b":"bee"},{"a":"zzz", "b":"ccc"}]' as | newContext index | }} 
    Foo {{newContext.a}} {{index}}
    {{/myeach}}
    tpl;

$renderer = Handlebars::compile($template, new Options(
    helpers: ['myeach' => $myEach],
));

echo $renderer(); // Foo ayy 0
                  // Foo zzz 1

This matches the API in Handlebars.js (playground demo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants