Skip to content

Commit

Permalink
support nested components of the same name.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 2, 2017
1 parent 82b4693 commit fcd9a0f
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/Illuminate/View/Concerns/ManagesComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function startComponent($name, array $data = [])
if (ob_start()) {
$this->componentStack[] = $name;

$this->componentData[$name] = $data;
$this->componentData[$this->currentComponent()] = $data;

$this->slots[$name] = [];
$this->slots[$this->currentComponent()] = [];
}
}

Expand All @@ -61,9 +61,7 @@ public function renderComponent()
{
$name = array_pop($this->componentStack);

return tap($this->make($name, $this->componentData($name))->render(), function () use ($name) {
$this->resetComponent($name);
});
return $this->make($name, $this->componentData($name))->render();
}

/**
Expand All @@ -74,9 +72,11 @@ public function renderComponent()
*/
protected function componentData($name)
{
$slot = ['slot' => new HtmlString(trim(ob_get_clean()))];

return array_merge($this->componentData[$name], $slot, $this->slots[$name]);
return array_merge(
$this->componentData[count($this->componentStack)],
['slot' => new HtmlString(trim(ob_get_clean()))],
$this->slots[count($this->componentStack)]
);
}

/**
Expand All @@ -88,9 +88,9 @@ protected function componentData($name)
public function slot($name)
{
if (ob_start()) {
$this->slots[last($this->componentStack)][$name] = '';
$this->slots[$this->currentComponent()][$name] = '';

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

Expand All @@ -103,21 +103,21 @@ public function endSlot()
{
$current = last($this->componentStack);

$currentSlot = array_pop($this->slotStack[$current]);
$currentSlot = array_pop(
$this->slotStack[$this->currentComponent()]
);

$this->slots[$current][$currentSlot] = new HtmlString(trim(ob_get_clean()));
$this->slots[$this->currentComponent()]
[$currentSlot] = new HtmlString(trim(ob_get_clean()));
}

/**
* Reset the state for the given component.
* Get the index for the current component.
*
* @param string $name
* @return void
* @return int
*/
protected function resetComponent($name)
protected function currentComponent()
{
unset($this->slots[$name]);
unset($this->slotStack[$name]);
unset($this->componentData[$name]);
return count($this->componentStack) - 1;
}
}

0 comments on commit fcd9a0f

Please sign in to comment.