跳到主要内容
版本:3.0

渲染钩子

概述

Filament 允许你在框架视图的多个节点渲染 Blade 模板内容。这使得插件可以注入 HTML 到框架中。同时,由于 Filament 不推荐发布视图因其可能会增加破坏性变更的风险,这也对用户很有用。

注册渲染钩子

要注册渲染钩子,你可以在服务提供者或者中间件中调用 FilamentView::registerRenderHook()。第一个参数是钩子名称,第二个参数返回要渲染的内容的回调函数:

use Filament\Support\Facades\FilamentView;
use Illuminate\Support\Facades\Blade;

FilamentView::registerRenderHook(
'panels::body.start',
fn (): string => Blade::render('@livewire(\'livewire-ui-modal\')'),
);

你也可以在文件中渲染视图内容:

use Filament\Support\Facades\FilamentView;
use Illuminate\Contracts\View\View;

FilamentView::registerRenderHook(
'panels::body.start',
fn (): View => view('impersonation-banner'),
);

可用渲染钩子

面板渲染钩子

  • panels::body.start - <body> 之后
  • panels::body.end - </body> 之前
  • panels::content.end - 页面内容之后,在 <main> 里面
  • panels::content.start - 页面内容之前,在 <main>
  • panels::footer - 页面 footer
  • panels::global-search.end - 全局搜索 容器尾部
  • panels::global-search.start - 全局搜索 容器开始处
  • panels::head.end - </head> 之前
  • panels::head.start - <head> 之后
  • panels::page.end - End of the page content container, also can be scoped to the page or resource class
  • panels::page.footer-widgets.after - After the page footer widgets, also can be scoped to the page or resource class
  • panels::page.footer-widgets.before - Before the page footer widgets, also can be scoped to the page or resource class
  • panels::page.header-widgets.after - After the page header widgets, also can be scoped to the page or resource class
  • panels::page.header-widgets.before - Before the page header widgets, also can be scoped to the page or resource class
  • panels::page.start - Start of the page content container, also can be scoped to the page or resource class
  • panels::resource.pages.list-records.table.after - After the resource table, also can be scoped to the page or resource class
  • panels::resource.pages.list-records.table.before - Before the resource table, also can be scoped to the page or resource class
  • panels::resource.pages.list-records.tabs.end - The end of the filter tabs (after the last tab), also can be scoped to the page or resource class
  • panels::resource.pages.list-records.tabs.start - The start of the filter tabs (before the first tab), also can be scoped to the page or resource class
  • panels::resource.relation-manager.after - After the relation manager table, also can be scoped to the page or relation manager class
  • panels::resource.relation-manager.before - Before the relation manager table, also can be scoped to the page or relation manager class
  • panels::scripts.after - After scripts are defined
  • panels::scripts.before - Before scripts are defined
  • panels::sidebar.nav.end - 在 侧边栏中,</nav> 之前
  • panels::sidebar.nav.start - 在 侧边栏中,</nav> 之后
  • panels::sidebar.footer - Pinned to the bottom of the sidebar, below the content
  • panels::styles.after - After styles are defined
  • panels::styles.before - Before styles are defined
  • panels::tenant-menu.after - 租户菜单之后
  • panels::tenant-menu.before - 租户菜单之前
  • panels::topbar.end - End of the topbar container
  • panels::topbar.start - Start of the topbar container
  • panels::user-menu.after - 用户菜单之后
  • panels::user-menu.before - 用户菜单之前
  • panels::user-menu.profile.after - After the profile item in the user menu
  • panels::user-menu.profile.before - Before the profile item in the user menu

渲染钩子作用域

有些渲染钩子可以限定作用范围,它让你可以只在某个特定页面或者 Livewire 组件中渲染。比如,你可能只想在一个页面中注册钩子。为此,你可以将页面类或者组件作为第二个参数传入到 registerRenderHook()

use Filament\Support\Facades\FilamentView;
use Illuminate\Support\Facades\Blade;

FilamentView::registerRenderHook(
'panels::page.start',
fn (): View => view('warning-banner'),
scopes: \App\Filament\Resources\UserResource\Pages\EditUser::class,
);

你也可以传入 scopes 数组:

use Filament\Support\Facades\FilamentView;

FilamentView::registerRenderHook(
'panels::page.start',
fn (): View => view('warning-banner'),
scopes: [
\App\Filament\Resources\UserResource\Pages\CreateUser::class,
\App\Filament\Resources\UserResource\Pages\EditUser::class,
],
);

有些面板构造器的渲染钩子允许你将钩子作用到资源的所有页面:

use Filament\Support\Facades\FilamentView;

FilamentView::registerRenderHook(
'panels::page.start',
fn (): View => view('warning-banner'),
scopes: \App\Filament\Resources\UserResource::class,
);

在渲染钩子中检索当前启用的作用域(scopes)

$scopes 变量被传入到渲染钩子函数中,你可以用它来确定在哪个页面或组件中对钩子进行渲染:

use Filament\Support\Facades\FilamentView;

FilamentView::registerRenderHook(
'panels::page.start',
fn (array $scopes): View => view('warning-banner', ['scopes' => $scopes]),
scopes: \App\Filament\Resources\UserResource::class,
);

渲染钩子

对插件开发者而言,将渲染钩子暴露给插件用户或许有用。你无需在任何地方注册这些钩子,只需在 Blade 中像这样输出:

{{ \Filament\Support\Facades\FilamentView::renderHook('panels::body.start') }}

要限定你的渲染钩子的作用域,你可以将其作为第二个参数传给 renderHook()。比如,如果钩子在 Livewire 组件内部,你可以使用 static::class 传递该组件类:

{{ \Filament\Support\Facades\FilamentView::renderHook('panels::page.start', scopes: $this->getRenderHookScopes()) }}

你也可以传递多个 scopes 作为数组,所有匹配该 scopes 的渲染钩子都会被渲染:

{{ \Filament\Support\Facades\FilamentView::renderHook('panels::page.start', scopes: [static::class, \App\Filament\Resources\UserResource::class]) }}