Skip to content

Commit 146d76d

Browse files
committed
debug
1 parent 07127f9 commit 146d76d

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/Validator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static function verify(Context $context, string $template): void
2525
Token::setDelimiter($context);
2626

2727
while (preg_match($context->tokenSearch, $template, $matches)) {
28+
var_dump($template); echo "\n";
29+
var_dump($context->level);
2830
// Skip a token when it is slash escaped
2931
if ($matches[Token::POS_LSPACE] === '' && preg_match('/^(.*?)(\\\\+)$/s', $matches[Token::POS_LOTHER], $escmatch)) {
3032
if (strlen($escmatch[2]) % 4) {
@@ -434,8 +436,12 @@ protected static function blockEnd(Context $context, array $vars, ?string $match
434436
$elseChain = array_shift($context->elseLvl);
435437
if (isset($elseChain[0])) {
436438
// we need to repeat a level due to else chains: {{else if}}
439+
var_dump($elseChain);
437440
$context->level++;
441+
var_dump($context->currentToken);
438442
$context->currentToken[Token::POS_RSPACE] = $context->currentToken[Token::POS_BACKFILL] = '{{/' . implode('}}{{/', $elseChain) . '}}' . Token::toString($context->currentToken) . $context->currentToken[Token::POS_RSPACE];
443+
var_dump($context->currentToken);
444+
exit;
439445
return Token::POS_BACKFILL;
440446
}
441447
// no break
@@ -444,6 +450,7 @@ protected static function blockEnd(Context $context, array $vars, ?string $match
444450
[$levels, $spvar, $var] = Expression::analyze($vars[0]);
445451
$v = Expression::toString($levels, $spvar, $var);
446452
if ($pop2 !== $v) {
453+
var_dump($v, $pop2);
447454
$context->error[] = 'Unexpect token ' . Token::toString($context->currentToken) . " ! Previous token {{{$pop}$pop2}} is not closed";
448455
return false;
449456
}

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)