Skip to content

Commit

Permalink
Merge pull request #54 from pug-php/feature/issue-53-mixins
Browse files Browse the repository at this point in the history
Fix #53 interpolateTwigFunctions end of stream
  • Loading branch information
kylekatarnls authored Jun 5, 2020
2 parents 8aac53f + 52d0578 commit f36cc82
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Pug/Symfony/Traits/HelpersHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,13 @@ protected function interpolateTwigFunctions(string $code): string
for ($index = 0; $index < $count; $index++) {
$token = $tokens[$index];

if (is_array($token) && $token[0] === T_STRING && $tokens[$index + 1] === '(') {
if (is_array($token) && $token[0] === T_STRING && ($tokens[$index + 1] ?? null) === '(') {
if ($token[1] === 'function_exists') {
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)])) {
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)])
) {
$output .= 'true';
$index += 3;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testCommand()
file_put_contents($customHelperFile, $customHelper);
file_put_contents($stylePhpFile, $stylePhp);

$this->assertStringContainsString('15 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
$this->assertStringContainsString('16 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
$this->assertStringContainsString('1 templates failed to be cached', $output, 'filter.pug fails as the upper filter does not exists.');
$this->assertRegExp('/(Unknown\sfilter\supper|upper:\sFilter\sdoes\s?n[\'o]t\sexists)/', $output, 'filter.pug fails as the upper filter does not exists.');
$this->assertStringContainsString('filter.pug', $output, 'filter.pug fails as the upper filter does not exists.');
Expand Down
12 changes: 12 additions & 0 deletions tests/Pug/PugSymfonyEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ public function testPreRenderPhp()
);
}

public function testMixin()
{
$pugSymfony = new PugSymfonyEngine(self::$kernel);
$pugSymfony->setOption('expressionLanguage', 'js');
$pugSymfony->setOption('prettyprint', false);

self::assertSame(
'<ul><li class="pet">cat</li><li class="pet">dog</li><li class="pet">pig</li></ul>',
$pugSymfony->render('mixin.pug')
);
}

/**
* @throws ErrorException
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/project-s5/templates/mixin.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mixin pet(name)
li.pet= name
ul
+pet('cat')
+pet('dog')
+pet('pig')

0 comments on commit f36cc82

Please sign in to comment.