Skip to content

Commit

Permalink
Add constants for layout render hooks (#193)
Browse files Browse the repository at this point in the history
* Define constants for render hooks in place of hard-coded strings for autocompletion

* Document `LayoutRenderHook` in the README

* Drop use of hard-coded strings for render hooks internally in favor of `LayoutRenderHook`
  • Loading branch information
Voltra authored Jan 1, 2025
1 parent 3fe52a1 commit fc65851
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 48 deletions.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ Apart from these this plugin also adds the following [Filament's Render Hooks](h

> Pro Tip 💡: Using a base layout is completely optional, if you don't need it you may just remove it from the generated layout blade file. If you prefer, You may also use your own base layout.
> Pro Tip 💡: You might prefer using the corresponding constants defined in `\Z3d0X\FilamentFabricator\View\LayoutRenderHook` instead of hard-coded strings.
## Page Blocks

### Creating a Page Block
Expand Down
100 changes: 52 additions & 48 deletions resources/views/components/layouts/base.blade.php
Original file line number Diff line number Diff line change
@@ -1,67 +1,71 @@
@props([
'title' => null,
'dir' => 'ltr'
'dir' => 'ltr',
])

@use(Z3d0X\FilamentFabricator\View\LayoutRenderHook)

<!DOCTYPE html>
<html
lang="{{ str_replace('_', '-', app()->getLocale()) }}"
dir="{{ $dir }}"
class="filament-fabricator"
>
<head>
{{ \Filament\Support\Facades\FilamentView::renderHook('filament-fabricator::head.start') }}

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">

@foreach (\Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getMeta() as $tag)
{{ $tag }}
@endforeach

@if ($favicon = \Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getFavicon())
<link rel="icon" href="{{ $favicon }}">
@endif
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" dir="{{ $dir }}" class="filament-fabricator">

<head>
{{ \Filament\Support\Facades\FilamentView::renderHook(LayoutRenderHook::HEAD_START) }}

<title>{{ $title ? "{$title} - " : null }} {{ config('app.name') }}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">

@foreach (\Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getMeta() as $tag)
{{ $tag }}
@endforeach

<style>
[x-cloak=""], [x-cloak="x-cloak"], [x-cloak="1"] { display: none !important; }
</style>
@if ($favicon = \Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getFavicon())
<link rel="icon" href="{{ $favicon }}">
@endif

<title>{{ $title ? "{$title} - " : null }} {{ config('app.name') }}</title>

@foreach (\Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getStyles() as $name => $path)
@if (\Illuminate\Support\Str::of($path)->startsWith('<'))
{!! $path !!}
@else
<link rel="stylesheet" href="{{ $path }}" />
@endif
@endforeach

{{ \Filament\Support\Facades\FilamentView::renderHook('filament-fabricator::head.end') }}
</head>
<style>
[x-cloak=""],
[x-cloak="x-cloak"],
[x-cloak="1"] {
display: none !important;
}
</style>

<body class="filament-fabricator-body">
{{ \Filament\Support\Facades\FilamentView::renderHook('filament-fabricator::body.start') }}

{{ $slot }}
@foreach (\Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getStyles() as $name => $path)
@if (\Illuminate\Support\Str::of($path)->startsWith('<'))
{!! $path !!}
@else
<link rel="stylesheet" href="{{ $path }}" />
@endif
@endforeach

{{ \Filament\Support\Facades\FilamentView::renderHook(LayoutRenderHook::HEAD_END) }}
</head>

{{ \Filament\Support\Facades\FilamentView::renderHook('filament-fabricator::scripts.start') }}
<body class="filament-fabricator-body">
{{ \Filament\Support\Facades\FilamentView::renderHook(LayoutRenderHook::BODY_START) }}

{{ $slot }}

{{ \Filament\Support\Facades\FilamentView::renderHook(LayoutRenderHook::SCRIPTS_START) }}

@foreach (\Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getScripts() as $name => $path)
@if (\Illuminate\Support\Str::of($path)->startsWith('<'))
{!! $path !!}
@else
<script defer src="{{ $path }}"></script>
@endif
@endforeach

@foreach (\Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getScripts() as $name => $path)
@if (\Illuminate\Support\Str::of($path)->startsWith('<'))
{!! $path !!}
@else
<script defer src="{{ $path }}"></script>
@endif
@endforeach
@stack('scripts')

@stack('scripts')
{{ \Filament\Support\Facades\FilamentView::renderHook(LayoutRenderHook::SCRIPTS_END) }}

{{ \Filament\Support\Facades\FilamentView::renderHook('filament-fabricator::scripts.end') }}
{{ \Filament\Support\Facades\FilamentView::renderHook(LayoutRenderHook::BODY_END) }}
</body>

{{ \Filament\Support\Facades\FilamentView::renderHook('filament-fabricator::body.end') }}
</body>
</html>
18 changes: 18 additions & 0 deletions src/View/LayoutRenderHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Z3d0X\FilamentFabricator\View;

class LayoutRenderHook
{
const HEAD_START = 'filament-fabricator::head.start';

const HEAD_END = 'filament-fabricator::head.end';

const BODY_START = 'filament-fabricator::body.start';

const SCRIPTS_START = 'filament-fabricator::scripts.start';

const SCRIPTS_END = 'filament-fabricator::scripts.end';

const BODY_END = 'filament-fabricator::body.end';
}

0 comments on commit fc65851

Please sign in to comment.