Skip to content

Commit

Permalink
[8.x] Add a BladeCompiler::renderComponent() method to render a compo…
Browse files Browse the repository at this point in the history
…nent instance (#40745)

* Add a BladeCompiler::renderComponent() method to render a component instance

* Fix test

* Update BladeCompiler.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
tobyzerner and taylorotwell authored Feb 2, 2022
1 parent 442b6d0 commit bcaebba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @method static bool check(string $name, array ...$parameters)
* @method static string compileString(string $value)
* @method static string render(string $string, array $data = [], bool $deleteCachedView = false)
* @method static string renderComponent(\Illuminate\View\Component $component)
* @method static string getPath()
* @method static string stripParentheses(string $expression)
* @method static void aliasComponent(string $path, string|null $alias = null)
Expand Down
26 changes: 26 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Illuminate\View\Compilers;

use Illuminate\Container\Container;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ReflectsClosures;
Expand Down Expand Up @@ -306,6 +308,30 @@ public function render()
});
}

/**
* Render a component instance to HTML.
*
* @param \Illuminate\View\Component $component
* @return string
*/
public static function renderComponent(Component $component)
{
$data = $component->data();

$view = value($component->resolveView(), $data);

if ($view instanceof View) {
return $view->with($data)->render();
} elseif ($view instanceof Htmlable) {
return $view->toHtml();
} else {
return Container::getInstance()
->make(ViewFactory::class)
->make($view, $data)
->render();
}
}

/**
* Store the blocks that do not receive compilation.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Integration/View/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View;
use Illuminate\View\Component;
use Orchestra\Testbench\TestCase;

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

public function test_rendering_blade_component_instance()
{
$component = new HelloComponent('Taylor');

$this->assertSame('Hello Taylor', Blade::renderComponent($component));
}

public function test_basic_blade_rendering()
{
$view = View::make('hello', ['name' => 'Taylor'])->render();
Expand Down Expand Up @@ -110,3 +118,18 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('view.paths', [__DIR__.'/templates']);
}
}

class HelloComponent extends Component
{
public $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function render()
{
return 'Hello {{ $name }}';
}
}

0 comments on commit bcaebba

Please sign in to comment.