Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize blade directive to allow changing the div id #186

Closed
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
32 changes: 32 additions & 0 deletions src/BladeDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Inertia;

class BladeDirective
{
public function render(string $id = 'app')
{
return new class($id) {
private $id;

private $page;

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

public function withPage($page)
{
$this->page = $page;

return $this;
}

public function __toString()
{
return '<div id="'.$this->id.'" data-page="'.e(json_encode($this->page)).'"></div>';
}
};
}
}
5 changes: 3 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ServiceProvider extends BaseServiceProvider
public function register()
{
$this->app->singleton(ResponseFactory::class);
$this->app->singleton(BladeDirective::class);
}

public function boot()
Expand All @@ -24,8 +25,8 @@ 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 = '') {
return '<?php echo app(\Inertia\BladeDirective::class)->render('.$expression.')->withPage($page); ?>';
});
}

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

namespace Inertia\Tests;

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

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')")
);
}

public function test_directive_is_rendered_with_a_custom_div_id_with_shorthand_if_expression()
{
$this->assertSame(
'<div id="foo" data-page="{&quot;foo&quot;:&quot;bar&quot;}"></div>',
$this->compileBlade("@inertia(true ? 'foo' : 'bar')")
);
}
}
4 changes: 3 additions & 1 deletion tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ 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->assertEquals('<?php echo app(\Inertia\BladeDirective::class)->render()->withPage($page); ?>', $directives['inertia']());
$this->assertEquals('<?php echo app(\Inertia\BladeDirective::class)->render("foo")->withPage($page); ?>', $directives['inertia']('"foo"'));
}

public function test_request_macro_is_registered()
Expand Down