Skip to content

Commit

Permalink
initial pass at components and slots
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 11, 2016
1 parent bd14251 commit e8d2a45
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,50 @@ protected function compileIncludeIf($expression)
return "<?php if (\$__env->exists($expression)) echo \$__env->make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
}

/**
* Compile the component statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileComponent($expression)
{
return "<?php \$__env->startComponent{$expression}; ?>";
}

/**
* Compile the end component statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndComponent($expression)
{
return "<?php echo \$__env->renderComponent(); ?>";
}

/**
* Compile the slot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileSlot($expression)
{
return "<?php \$__env->slot{$expression}; ?>";
}

/**
* Compile the end slot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndSlot($expression)
{
return "<?php \$__env->endSlot(); ?>";
}

/**
* Compile the stack statements into the content.
*
Expand Down
99 changes: 99 additions & 0 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Support\HtmlString;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\Contracts\Events\Dispatcher;
Expand Down Expand Up @@ -93,6 +94,34 @@ class Factory implements FactoryContract
*/
protected $sectionStack = [];

/**
* The components being rendered.
*
* @var array
*/
protected $componentStack = [];

/**
* The original data passed to the component.
*
* @var array
*/
protected $componentData = [];

/**
* The slot contents for the component.
*
* @var array
*/
protected $slots = [];

/**
* The names of the slots being rendered.
*
* @var array
*/
protected $slotStack = [];

/**
* The stack of in-progress loops.
*
Expand Down Expand Up @@ -680,6 +709,75 @@ public static function parentPlaceholder()
return static::$parentPlaceholder;
}

/**
* Start a component rendering process.
*
* @param string $name
* @param array $data
* @return void
*/
public function startComponent($name, array $data = [])
{
if (ob_start()) {
$this->componentStack[] = $name;

$this->componentData[$name] = $data;

$this->slots[$name] = [];
}
}

/**
* Render the current component.
*
* @return string
*/
public function renderComponent()
{
$contents = ob_get_clean();

$name = array_pop($this->componentStack);

$baseData = $this->componentData[$name];

$data = array_merge(
$baseData, ['slot' => new HtmlString($contents)], $this->slots[$name]
);

return tap($this->make($name, $data)->render(), function () use ($name) {
unset($this->slots[$name]);
unset($this->slotStack[$name]);
unset($this->componentData[$name]);
});
}

/**
* Start the slot rendering process.
*
* @param string $name
* @return void
*/
public function slot($name)
{
if (ob_start()) {
$this->slots[last($this->componentStack)][$name] = '';

$this->slotStack[last($this->componentStack)][] = $name;
}
}

/**
* Save the slot content for rendering.
*
* @return void
*/
public function endSlot()
{
$current = last($this->componentStack);

$this->slots[$current][array_pop($this->slotStack[$current])] = new HtmlString(ob_get_clean());
}

/**
* Start injecting content into a push section.
*
Expand Down Expand Up @@ -729,6 +827,7 @@ protected function extendPush($section, $content)
if (! isset($this->pushes[$section])) {
$this->pushes[$section] = [];
}

if (! isset($this->pushes[$section][$this->renderCount])) {
$this->pushes[$section][$this->renderCount] = $content;
} else {
Expand Down

0 comments on commit e8d2a45

Please sign in to comment.