Skip to content

Commit bcaebba

Browse files
[8.x] Add a BladeCompiler::renderComponent() method to render a component instance (#40745)
* Add a BladeCompiler::renderComponent() method to render a component instance * Fix test * Update BladeCompiler.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 442b6d0 commit bcaebba

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/Illuminate/Support/Facades/Blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @method static bool check(string $name, array ...$parameters)
1010
* @method static string compileString(string $value)
1111
* @method static string render(string $string, array $data = [], bool $deleteCachedView = false)
12+
* @method static string renderComponent(\Illuminate\View\Component $component)
1213
* @method static string getPath()
1314
* @method static string stripParentheses(string $expression)
1415
* @method static void aliasComponent(string $path, string|null $alias = null)

src/Illuminate/View/Compilers/BladeCompiler.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Illuminate\View\Compilers;
44

55
use Illuminate\Container\Container;
6+
use Illuminate\Contracts\Support\Htmlable;
67
use Illuminate\Contracts\View\Factory as ViewFactory;
8+
use Illuminate\Contracts\View\View;
79
use Illuminate\Support\Arr;
810
use Illuminate\Support\Str;
911
use Illuminate\Support\Traits\ReflectsClosures;
@@ -306,6 +308,30 @@ public function render()
306308
});
307309
}
308310

311+
/**
312+
* Render a component instance to HTML.
313+
*
314+
* @param \Illuminate\View\Component $component
315+
* @return string
316+
*/
317+
public static function renderComponent(Component $component)
318+
{
319+
$data = $component->data();
320+
321+
$view = value($component->resolveView(), $data);
322+
323+
if ($view instanceof View) {
324+
return $view->with($data)->render();
325+
} elseif ($view instanceof Htmlable) {
326+
return $view->toHtml();
327+
} else {
328+
return Container::getInstance()
329+
->make(ViewFactory::class)
330+
->make($view, $data)
331+
->render();
332+
}
333+
}
334+
309335
/**
310336
* Store the blocks that do not receive compilation.
311337
*

tests/Integration/View/BladeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\Facades\View;
7+
use Illuminate\View\Component;
78
use Orchestra\Testbench\TestCase;
89

910
class BladeTest extends TestCase
@@ -13,6 +14,13 @@ public function test_rendering_blade_string()
1314
$this->assertSame('Hello Taylor', Blade::render('Hello {{ $name }}', ['name' => 'Taylor']));
1415
}
1516

17+
public function test_rendering_blade_component_instance()
18+
{
19+
$component = new HelloComponent('Taylor');
20+
21+
$this->assertSame('Hello Taylor', Blade::renderComponent($component));
22+
}
23+
1624
public function test_basic_blade_rendering()
1725
{
1826
$view = View::make('hello', ['name' => 'Taylor'])->render();
@@ -110,3 +118,18 @@ protected function getEnvironmentSetUp($app)
110118
$app['config']->set('view.paths', [__DIR__.'/templates']);
111119
}
112120
}
121+
122+
class HelloComponent extends Component
123+
{
124+
public $name;
125+
126+
public function __construct(string $name)
127+
{
128+
$this->name = $name;
129+
}
130+
131+
public function render()
132+
{
133+
return 'Hello {{ $name }}';
134+
}
135+
}

0 commit comments

Comments
 (0)