Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public function boot()

protected function registerBladeDirective()
{
Blade::directive('inertia', function () {
return '<div id="app" data-page="{{ json_encode($page) }}"></div>';
Blade::directive('inertia', function ($expression) {
$appId = $expression ?: "'app'";

return '<div id="{{ '.$appId.' }}" data-page="{{ json_encode($page) }}"></div>';
});
}

Expand Down
47 changes: 47 additions & 0 deletions tests/BladeDirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Inertia\Tests;

use Mockery as m;
use Illuminate\Support\Facades\Blade;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;

class BladeDirectiveTest extends TestCase
{
protected function compileBlade(string $expression, $page = ['foo' => 'bar'])
{
$compiler = new BladeCompiler(m::mock(Filesystem::class), __DIR__);
$compiler->directive('inertia', Blade::getCustomDirectives()['inertia']);

$compiled = tap($compiler->compileString($expression), function () {
m::close();
});

ob_start(function () {
// This closure exists to prevent 'eval' output from getting sent to
// STDOUT, which in turn causes PHPUnit to complain about it.
return '';
});

eval("?>$compiled<?php ");

return ob_get_flush();
}

public function test_directive_is_rendered_with_the_default_options()
{
$this->assertSame(
'<div id="app" data-page="{&quot;foo&quot;:&quot;bar&quot;}"></div>',
$this->compileBlade('@inertia')
);
}

public function test_directive_is_rendered_with_a_custom_div_id()
{
$this->assertSame(
'<div id="foo" data-page="{&quot;foo&quot;:&quot;bar&quot;}"></div>',
$this->compileBlade("@inertia('foo')")
);
}
}
5 changes: 1 addition & 4 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ class ServiceProviderTest extends TestCase
{
public function test_blade_directive_is_registered()
{
$directives = Blade::getCustomDirectives();

$this->assertArrayHasKey('inertia', $directives);
$this->assertEquals('<div id="app" data-page="{{ json_encode($page) }}"></div>', $directives['inertia']());
$this->assertArrayHasKey('inertia', Blade::getCustomDirectives());
}

public function test_request_macro_is_registered()
Expand Down