File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed
Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 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)
Original file line number Diff line number Diff line change 33namespace Illuminate \View \Compilers ;
44
55use Illuminate \Container \Container ;
6+ use Illuminate \Contracts \Support \Htmlable ;
67use Illuminate \Contracts \View \Factory as ViewFactory ;
8+ use Illuminate \Contracts \View \View ;
79use Illuminate \Support \Arr ;
810use Illuminate \Support \Str ;
911use 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 *
Original file line number Diff line number Diff line change 44
55use Illuminate \Support \Facades \Blade ;
66use Illuminate \Support \Facades \View ;
7+ use Illuminate \View \Component ;
78use Orchestra \Testbench \TestCase ;
89
910class 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+ }
You can’t perform that action at this time.
0 commit comments