Skip to content

Commit f36cc82

Browse files
authored
Merge pull request #54 from pug-php/feature/issue-53-mixins
Fix #53 interpolateTwigFunctions end of stream
2 parents 8aac53f + 52d0578 commit f36cc82

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/Pug/Symfony/Traits/HelpersHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,13 @@ protected function interpolateTwigFunctions(string $code): string
196196
for ($index = 0; $index < $count; $index++) {
197197
$token = $tokens[$index];
198198

199-
if (is_array($token) && $token[0] === T_STRING && $tokens[$index + 1] === '(') {
199+
if (is_array($token) && $token[0] === T_STRING && ($tokens[$index + 1] ?? null) === '(') {
200200
if ($token[1] === 'function_exists') {
201-
if ($tokens[$index + 3] === ')' && is_array($tokens[$index + 2]) && $tokens[$index + 2][0] === T_CONSTANT_ENCAPSED_STRING && isset($this->twigHelpers[substr($tokens[$index + 2][1], 1, -1)])) {
201+
if ($tokens[$index + 3] === ')' &&
202+
is_array($tokens[$index + 2]) &&
203+
$tokens[$index + 2][0] === T_CONSTANT_ENCAPSED_STRING &&
204+
isset($this->twigHelpers[substr($tokens[$index + 2][1], 1, -1)])
205+
) {
202206
$output .= 'true';
203207
$index += 3;
204208
continue;

tests/Pug/PugSymfonyBundle/Command/AssetsPublishCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testCommand()
4545
file_put_contents($customHelperFile, $customHelper);
4646
file_put_contents($stylePhpFile, $stylePhp);
4747

48-
$this->assertStringContainsString('15 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
48+
$this->assertStringContainsString('16 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
4949
$this->assertStringContainsString('1 templates failed to be cached', $output, 'filter.pug fails as the upper filter does not exists.');
5050
$this->assertRegExp('/(Unknown\sfilter\supper|upper:\sFilter\sdoes\s?n[\'o]t\sexists)/', $output, 'filter.pug fails as the upper filter does not exists.');
5151
$this->assertStringContainsString('filter.pug', $output, 'filter.pug fails as the upper filter does not exists.');

tests/Pug/PugSymfonyEngineTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ public function testPreRenderPhp()
146146
);
147147
}
148148

149+
public function testMixin()
150+
{
151+
$pugSymfony = new PugSymfonyEngine(self::$kernel);
152+
$pugSymfony->setOption('expressionLanguage', 'js');
153+
$pugSymfony->setOption('prettyprint', false);
154+
155+
self::assertSame(
156+
'<ul><li class="pet">cat</li><li class="pet">dog</li><li class="pet">pig</li></ul>',
157+
$pugSymfony->render('mixin.pug')
158+
);
159+
}
160+
149161
/**
150162
* @throws ErrorException
151163
*/

tests/project-s5/templates/mixin.pug

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mixin pet(name)
2+
li.pet= name
3+
ul
4+
+pet('cat')
5+
+pet('dog')
6+
+pet('pig')

0 commit comments

Comments
 (0)