Custom helper not working as expected
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
Research direction
No project file or test is named. Reproduce the shown addHelper and render example, then inspect how custom helper return values and block rendering are handled; done means the example produces only 8 instead of 1189.
Written by the indexing model from the issue text.
Description
I've created a custom helper that evaulates a conditional expression (similar to if with conditional expression) as below:
$this->handlebars->addHelper("compare",
function($template, $context, $args, $source){
list($operandSpecName, $operator, $operand2) = explode(" ",$args);
$operand1 = $context->get($operandSpecName);
$operator = str_replace("'", "", $operator);
$operand2 = str_replace("'", "", $operand2);
switch($operator){
case '==':
return ($operand1 == $operand2) ? true: false;
case '===':
return ($operand1 === $operand2) ? true: false;
case '!=':
return ($operand1 != $operand2) ? true: false;
case '!==':
return ($operand1 !== $operand2) ? true: false;
default:
return false;
}
}
);
Here's my template:
$template = `"{{#compare comics '==' 'Marvel'}}{{#compare movie '==' 'Avenger'}}8{{\/compare}}{{\/compare}}{{#compare comics '==' 'DC'}}{{#compare movie '==' 'Batman'}}9{{\/compare}}{{\/compare}}"
And here's the data I've passed:
$data = [
"comics" => "Marvel",
"movie" => "Avenger"
];
I've called the render function as below:
$this->handlebars->render($template, $data);
I expect the result of expression as 8 since only the first two expression of compare helper are evaluated as being true.
However, I get the output as 1189. I noticed that the helper returns 1 each time it evaluates a true condition and it also outputs result even if the codition does not evaluates true.
I was wondering if anyone can help me to correct the logic of my custom helper?
Thanks!
- Dominant language
- PHP
- Stars
- 77
- Forks
- 34
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from salesforce/handlebars-php
-
Difficulty 4/5 3-5 days Newbie friendliness 57/100
salesforce/handlebars-php#32 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
salesforce/handlebars-php#25 · 2 comments · 2 reactions ·
-
rendering with strict=true does not throw for missing nested variables in contextPossibly taken @hubermat claimed this 1151 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 25/100
salesforce/handlebars-php#23 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
salesforce/handlebars-php#22 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
salesforce/handlebars-php#21 · 1 comment · 4 reactions ·
All issues in salesforce/handlebars-php
Similar issues
-
bug good first issue small
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
open-telemetry/opentelemetry-php#2164 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
-
sync-en
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
Перевод устарел
Difficulty 1/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Documentation
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day