Skip to content

Commit c332e4b

Browse files
committed
Simplify parsing else if
1 parent de5f971 commit c332e4b

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ HandleBars change log
33

44
## ?.?.? / ????-??-??
55

6+
* Simplify code for parsing `else if` - @thekid
67
* Added PHP 8.6 to test matrix - @thekid
78

89
## 10.0.1 / 2025-09-06

src/main/php/com/handlebarsjs/BlockHelpers.class.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function register($name, $impl) {
2323
unset($this->impl[$name]);
2424
} else if (is_string($impl)) {
2525
$this->impl[$name]= function($options, $state) use($impl) {
26-
return $state->target->add(new $impl($options, null, null, $state->start, $state->end));
26+
return new $impl($options, null, null, $state->start, $state->end);
2727
};
2828
} else {
2929
$this->impl[$name]= $impl;
@@ -35,7 +35,6 @@ public function register($name, $impl) {
3535
* Creates a new with block helper
3636
*
3737
* - Creates instances of named block implementations
38-
* - Registers `*inline` partials in top-level nodes
3938
* - Uses default block implementation otherwise
4039
*
4140
* @param var[] $options
@@ -44,10 +43,10 @@ public function register($name, $impl) {
4443
*/
4544
public function newInstance($options, $state) {
4645
$name= array_shift($options);
47-
if ($impl= $this->impl[$name] ?? null) {
46+
if ($impl= $this->impl[(string)$name] ?? null) {
4847
return $impl($options, $state);
4948
} else {
50-
return $state->target->add(new BlockNode($name, $options, null, null, $state->start, $state->end));
49+
return new BlockNode($name, $options, null, null, $state->start, $state->end);
5150
}
5251
}
5352
}

src/main/php/com/handlebarsjs/HandlebarsParser.class.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ protected function initialize() {
128128
// Sections
129129
$this->withHandler('#', true, function($tag, $state, $parse) {
130130
$state->parents[]= $state->target;
131-
$block= $this->blocks->newInstance($parse->options(trim(substr($tag, 1))), $state);
131+
$block= $state->target->add($this->blocks->newInstance(
132+
$parse->options(trim(substr($tag, 1))),
133+
$state
134+
));
132135
$state->target= $block->fn();
133136
$state->parents[]= $block;
134137
});
@@ -204,7 +207,10 @@ protected function initialize() {
204207
$state->target= $block->inverse();
205208
} else {
206209
$state->parents[]= $state->target;
207-
$block= new InverseOf($this->blocks->newInstance($parse->options(substr($tag, 1)), $state));
210+
$block= new InverseOf($state->target->add($this->blocks->newInstance(
211+
$parse->options(substr($tag, 1)),
212+
$state
213+
)));
208214
$state->target= $block->fn();
209215
$state->parents[]= $block;
210216
}
@@ -223,14 +229,11 @@ protected function initialize() {
223229
$context= &$state->parents[sizeof($state->parents) - 1];
224230
if ($context instanceof BlockNode) {
225231

226-
// `else if` vs. `else`
227-
if (isset($parsed[1]) && 'if' === (string)$parsed[1]) {
228-
$context= $context->inverse()->add(new IfBlockHelper(
229-
array_slice($parsed, 2),
230-
null,
231-
null,
232-
$state->start,
233-
$state->end
232+
// `else [...]` vs. `else`
233+
if (isset($parsed[1])) {
234+
$context= $context->inverse()->add($this->blocks->newInstance(
235+
array_slice($parsed, 1),
236+
$state
234237
));
235238
$state->target= $context->fn();
236239
} else {

0 commit comments

Comments
 (0)