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 Block Helper with blockParams #360

Open
toovy opened this issue Feb 13, 2022 · 0 comments
Open

Custom Block Helper with blockParams #360

toovy opened this issue Feb 13, 2022 · 0 comments

Comments

@toovy
Copy link

toovy commented Feb 13, 2022

Hi!

I'd like to use block parameters in my own helper. In my example scenario I want to add two values and "store" them as block variable "result" with a "let" helper (inspired by the ember.js let helper https://guides.emberjs.com/release/components/helper-functions/#toc_the-let-helper). Looking at the handlebars "with"-helper (https://github.com/handlebars-lang/handlebars.js/blob/master/lib/handlebars/helpers/with.js) I tried to create the following code where I expect result to be 3.

In my case this fails, but I can't figure out how blockParams works exactly and I could not find any lightncandy docs for it. I know that I could switch the context and just use {{.}}, but I hate the context switching feature and want to explicitely name the new I looked at the docs here https://zordius.github.io/HandlebarsCookbook/0022-blockhelper.html and here https://zordius.github.io/HandlebarsCookbook/9002-helperoptions.html .

Any help would be appreciated. Thanks!

$template = <<<EOF
{{#let (add 1 2) as |result|}}
  Result is: {{result}}
{{/let}}
EOF;

$phpStr = LightnCandy::compile($template, [
  'flags' => LightnCandy::FLAG_HANDLEBARS,
  'helpers' => [
    "add" => function ($a, $b) {
      return $a + $b;
    },
    'let' => function ($context, $options) {
      return $options['fn']($context, [
        "data" => $options['data'],
        "blockParams" => [$context]
      ]);
    }
  ]
]);
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

1 participant