Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Fix Blade @parent issue #16033

Merged
merged 2 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,16 @@ public function compileEchoDefaults($value)
return preg_replace('/^(?=\$)(.+?)(?:\s+or\s+)(.+?)$/s', 'isset($1) ? $1 : $2', $value);
}

/**
* Replace the @parent directive to a placeholder.
*
* @return string
*/
protected function compileParent()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't seem to be used anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
return '##parent-placeholder##';
}

/**
* Compile the each statements into valid PHP.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ public function appendSection()
protected function extendSection($section, $content)
{
if (isset($this->sections[$section])) {
$content = str_replace('@parent', $content, $this->sections[$section]);
$content = str_replace('##parent-placeholder##', $content, $this->sections[$section]);
}

$this->sections[$section] = $content;
Expand All @@ -655,7 +655,7 @@ public function yieldContent($section, $default = '')
$sectionContent = str_replace('@@parent', '--parent--holder--', $sectionContent);

return str_replace(
'--parent--holder--', '@parent', str_replace('@parent', '', $sectionContent)
'--parent--holder--', '@parent', str_replace('##parent-placeholder##', '', $sectionContent)
);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function testSectionExtending()
{
$factory = $this->getFactory();
$factory->startSection('foo');
echo 'hi @parent';
echo 'hi ##parent-placeholder##';
$factory->stopSection();
$factory->startSection('foo');
echo 'there';
Expand All @@ -257,10 +257,10 @@ public function testSectionMultipleExtending()
{
$factory = $this->getFactory();
$factory->startSection('foo');
echo 'hello @parent nice to see you @parent';
echo 'hello ##parent-placeholder## nice to see you ##parent-placeholder##';
$factory->stopSection();
$factory->startSection('foo');
echo 'my @parent';
echo 'my ##parent-placeholder##';
$factory->stopSection();
$factory->startSection('foo');
echo 'friend';
Expand Down
29 changes: 28 additions & 1 deletion tests/View/ViewFlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,18 @@ public function testExtendsWithParent()
@section("me")
@parent
child
@endsection'));

$files->put($this->tempDir.'/extends-child-child.php', $compiler->compileString('
@extends("extends-child")
@section("me")
@parent
child-child
@endsection'));

$factory = $this->prepareCommonFactory();
$this->assertEquals("yield:\ndad\n\nchild\n", $factory->make('extends-child')->render());
$this->assertEquals("yield:\ndad\n\nchild\n\nchild-child\n", $factory->make('extends-child-child')->render());
}

public function testExtendsWithVariable()
Expand Down Expand Up @@ -180,7 +188,26 @@ public function testExtendsWithVariable()

$factory = $this->prepareCommonFactory();
$this->assertEquals("yield:\ntitle\n", $factory->make('extends-variable-child-a', ['title' => 'title'])->render());
$this->assertEquals("yield:\ndad\n\n", $factory->make('extends-variable-child-b', ['title' => '@parent'])->render());
$this->assertEquals("yield:\n@parent\n", $factory->make('extends-variable-child-b', ['title' => '@parent'])->render());
}

public function testExtendsWithVariableAndNoParent()
{
$files = new Filesystem;
$compiler = new BladeCompiler($this->getFiles(), __DIR__);

$files->put($this->tempDir.'/extends-variable-layout.php', $compiler->compileString('
yield:
@yield("me")'));

$files->put($this->tempDir.'/extends-variable.php', $compiler->compileString('
@extends("extends-variable-layout")
@section("me")
{{ $title }}
@endsection'));

$factory = $this->prepareCommonFactory();
$this->assertEquals("yield:\n@parent\n", $factory->make('extends-variable', ['title' => '@parent'])->render());
}

protected function prepareCommonFactory()
Expand Down