Skip to content

Commit

Permalink
Parity with slot contents
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Oct 30, 2024
1 parent 02fe069 commit dba197e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/Tags/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\Tags;

use Illuminate\Support\HtmlString;

class Partial extends Tags
{
public function wildcard($tag)
Expand All @@ -21,14 +23,25 @@ protected function render($partial)

$variables = array_merge($this->context->all(), $this->params->all(), [
'__frontmatter' => $this->params->all(),
'slot' => $this->isPair ? trim($this->parse()) : null,
'slot' => $this->isPair ? $this->getSlotContent() : null,
]);

return view($this->viewName($partial), $variables)
->withoutExtractions()
->render();
}

protected function getSlotContent()
{
$content = trim($this->parse());

if ($this->isAntlersBladeComponent()) {
return new HtmlString($content);
}

return $content;
}

protected function shouldRender(): bool
{
if ($this->params->has('when')) {
Expand Down
2 changes: 1 addition & 1 deletion src/View/Blade/Concerns/CompilesPartials.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function compilePartial(ComponentNode $component): string
$injectedParam->setName($name);
$injectedParam->type = ParameterType::DynamicVariable;

$injectedParam->value = '\Illuminate\Support\Facades\Blade::render($'.$hoistedVarName.', get_defined_vars())';
$injectedParam->value = 'new \Illuminate\Support\HtmlString(\Illuminate\Support\Facades\Blade::render($'.$hoistedVarName.', get_defined_vars()))';

$hoistedSet .= Str::swap([
'$varName' => $hoistedVarName,
Expand Down
46 changes: 46 additions & 0 deletions tests/View/Blade/AntlersComponents/PartialCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,50 @@ public function it_compiles_nested_self_closing_partial_tags()
Str::squish(Blade::render($template))
);
}

#[Test]
public function slot_content_does_not_need_to_be_manually_escaped()
{
$this->withFakeViews();

$partial = <<<'BLADE'
Partial Start {{ $slot }} Partial End
BLADE;

$this->viewShouldReturnRaw('the_partial', $partial, 'blade.php');

$template = <<<'BLADE'
<s:partial:the_partial>
<strong>I am the slot content!</strong>
</s:partial:the_partial>
BLADE;

$this->assertSame(
'Partial Start <strong>I am the slot content!</strong> Partial End',
Blade::render($template),
);
$partial = <<<'BLADE'
Header Start {{ $header }} Header End
Partial Start {{ $slot }} Partial End
BLADE;

$this->viewShouldReturnRaw('the_partial', $partial, 'blade.php');

$template = <<<'BLADE'
<s:partial:the_partial>
<s:slot:header>I am <em>the header!</em></s:slot:header>
<strong>I am the slot content!</strong>
</s:partial:the_partial>
BLADE;

$expected = <<<'EXPECTED'
Header Start I am <em>the header!</em> Header End
Partial Start <strong>I am the slot content!</strong> Partial End
EXPECTED;

$this->assertSame(
$expected,
Blade::render($template),
);
}
}

0 comments on commit dba197e

Please sign in to comment.