Skip to content

Commit 85bf8fc

Browse files
committed
Fix spacing and undefined variable for each block in partial
Resolves #7
1 parent 604a28b commit 85bf8fc

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ protected static function section(Context $context, array $vars, bool $isEach =
406406
$v = static::getVariableNameOrSubExpression($context, $vars[0]);
407407
$each = $isEach ? 'true' : 'false';
408408
return $context->separator . static::getFuncName($context, 'sec', ($isEach ? 'each ' : '') . $v[1] . $be)
409-
. "\$cx, {$v[0]}, $bs, \$in, $each, function(\$cx, \$in) {{$context->fStart}";
409+
. "\$cx, {$v[0]}, $bs, \$in, $each, function(\$cx, \$in) use (&\$sp) {{$context->fStart}\$sp.";
410410
}
411411

412412
/**

src/Partial.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public static function compile(Context $context, string $template, string $name)
9898
$code = preg_replace('/^/m', "'{$context->separator}\$sp{$context->separator}'", $code);
9999
// remove extra spaces before partial
100100
$code = preg_replace('/^\'\\.\\$sp\\.\'(\'\\.LR::p\\()/m', '$1', $code, 1);
101+
// remove extra spaces before section
102+
$code = preg_replace('/^\'\\.\\$sp\\.\'(\'\\.LR::sec\\()/m', '$1', $code, 1);
103+
// remove extra spaces before blank lines
104+
$code = preg_replace('/^\'\\.\\$sp\\.\'(\';}\\))/m', '$1', $code, 1);
101105
// add spaces after partial
102106
$code = preg_replace('/^(\'\\.LR::p\\(.+\\)\\.)(\'.+)/m', '$1\$sp.$2', $code, 1);
103107
}

tests/RegressionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ public static function issueProvider(): array
9595
};
9696

9797
$issues = [
98+
[
99+
'id' => 7,
100+
'template' => "<p>\n {{> list}}\n</p>",
101+
'data' => ['items' => ['Hello', 'World']],
102+
'options' => new Options(
103+
partials: ['list' => "{{#each items}}{{this}}\n{{/each}}"],
104+
),
105+
'expected' => "<p>\n Hello\n World\n</p>",
106+
],
107+
98108
[
99109
'id' => 39,
100110
'template' => '{{{tt}}}',

0 commit comments

Comments
 (0)