Skip to content

Commit 7f75828

Browse files
committed
debug
1 parent e81e06e commit 7f75828

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/Validator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,9 @@ protected static function blockEnd(Context $context, array $vars, ?string $match
435435
if (isset($elseChain[0])) {
436436
// we need to repeat a level due to else chains: {{else if}}
437437
$context->level++;
438-
$context->currentToken[Token::POS_RSPACE] = $context->currentToken[Token::POS_BACKFILL] = '{{/' . implode('}}{{/', $elseChain) . '}}' . Token::toString($context->currentToken) . $context->currentToken[Token::POS_RSPACE];
438+
$context->currentToken[Token::POS_RSPACE] = $context->currentToken[Token::POS_BACKFILL]
439+
= '{{/' . implode('}}{{/', $elseChain) . '}}'
440+
. Token::toString($context->currentToken) . $context->currentToken[Token::POS_RSPACE];
439441
return Token::POS_BACKFILL;
440442
}
441443
// no break
@@ -444,6 +446,7 @@ protected static function blockEnd(Context $context, array $vars, ?string $match
444446
[$levels, $spvar, $var] = Expression::analyze($vars[0]);
445447
$v = Expression::toString($levels, $spvar, $var);
446448
if ($pop2 !== $v) {
449+
//echo "Unexpect token: $v $pop2 \n";
447450
$context->error[] = 'Unexpect token ' . Token::toString($context->currentToken) . " ! Previous token {{{$pop}$pop2}} is not closed";
448451
return false;
449452
}

template.hbs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{#ifEquals @root.item "foo"}}
2+
phooey
3+
{{else ifEquals @root.item "bar"}}
4+
barry
5+
{{else}}
6+
{{#if @root.item}}
7+
{{#ifEquals @root.item "fizz"}}
8+
fizzy
9+
{{else ifEquals @root.item "buzz"}}
10+
buzzy
11+
{{else}}
12+
no matches
13+
{{/ifEquals}}
14+
{{/if}}
15+
{{/ifEquals}}

test2.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use DevTheorem\Handlebars\{Handlebars, HelperOptions, Options};
4+
5+
require 'vendor/autoload.php';
6+
7+
$file = file_get_contents('template.hbs');
8+
$templateString = $file;
9+
10+
$code = Handlebars::precompile($templateString, new Options(
11+
helpers: [
12+
'ifEquals' => function (mixed $a, mixed $b, HelperOptions $options) {
13+
$jsEquals = function (mixed $a, mixed $b): bool {
14+
if ($a === null || $b === null) {
15+
// in JS, null is not equal to blank string or false or zero
16+
return $a === $b;
17+
}
18+
19+
return $a == $b;
20+
};
21+
22+
return $jsEquals($a, $b) ? $options->fn() : $options->inverse();
23+
},
24+
],
25+
));
26+
27+
echo $code . "\n\n";
28+
29+
$template = Handlebars::template($code);
30+
31+
echo $template([
32+
'item' => 'buzz',
33+
]);

0 commit comments

Comments
 (0)