Skip to content

Commit

Permalink
working on parent placeholderS
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 24, 2016
1 parent b35d334 commit 16f72a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\View\Factory as ViewFactory;

class BladeCompiler extends Compiler implements CompilerInterface
{
Expand Down Expand Up @@ -411,7 +412,7 @@ public function compileEchoDefaults($value)
*/
protected function compileParent()
{
return '##parent-placeholder##';
return ViewFactory::parentPlaceholder();
}

/**
Expand Down
25 changes: 23 additions & 2 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ class Factory implements FactoryContract
*/
protected $renderCount = 0;

/**
* The parent placeholder for the request.
*
* @var string
*/
protected static $parentPlaceholder;

/**
* Create a new view factory instance.
*
Expand Down Expand Up @@ -631,7 +638,7 @@ public function appendSection()
protected function extendSection($section, $content)
{
if (isset($this->sections[$section])) {
$content = str_replace('##parent-placeholder##', $content, $this->sections[$section]);
$content = str_replace(static::parentPlaceholder(), $content, $this->sections[$section]);
}

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

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

/**
* Get the parent placeholder for the current request.
*
* @return string
*/
public static function parentPlaceholder()
{
if (! static::$parentPlaceholder) {
static::$parentPlaceholder = '##parent-placeholder-'.Str::random(40).'##';
}

return static::$parentPlaceholder;
}

/**
* Start injecting content into a push section.
*
Expand Down
8 changes: 5 additions & 3 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@ public function testBasicSectionHandling()

public function testSectionExtending()
{
$placeholder = Illuminate\View\Factory::parentPlaceholder();
$factory = $this->getFactory();
$factory->startSection('foo');
echo 'hi ##parent-placeholder##';
echo 'hi '.$placeholder;
$factory->stopSection();
$factory->startSection('foo');
echo 'there';
Expand All @@ -255,12 +256,13 @@ public function testSectionExtending()

public function testSectionMultipleExtending()
{
$placeholder = Illuminate\View\Factory::parentPlaceholder();
$factory = $this->getFactory();
$factory->startSection('foo');
echo 'hello ##parent-placeholder## nice to see you ##parent-placeholder##';
echo 'hello '.$placeholder.' nice to see you '.$placeholder;
$factory->stopSection();
$factory->startSection('foo');
echo 'my ##parent-placeholder##';
echo 'my '.$placeholder;
$factory->stopSection();
$factory->startSection('foo');
echo 'friend';
Expand Down

0 comments on commit 16f72a5

Please sign in to comment.