travelopia / wordpress-blade
在WordPress中使用Laravel Blade组件。
1.0.1
2024-08-22 06:27 UTC
Requires
- illuminate/view: ^v10.48.20
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-23 22:55:33 UTC
README
在WordPress中使用Laravel Blade组件。
🚨 注意:目前仅支持匿名组件:[点击查看详情](https://laravel.net.cn/docs/10.x/blade#anonymous-components)
安装
通过Composer安装(推荐)
$ composer require travelopia/wordpress-blade
这将将其作为MU插件安装。加载并按需使用!
手动安装(如果您知道自己在做什么)
- 下载此存储库的ZIP文件。
- 运行
composer install --no-dev --optimize-autoloader
- 您可以使用它作为MU插件或普通插件!
为生产构建
作为最佳实践,编译您的Blade组件以供生产使用。一些生产环境是只读的,在这种情况下此步骤是必要的。
运行以下命令
composer exec wordpress-blade -- --config-file=blade.config.php
- 确保Blade配置文件的路径正确。
使用方法
首先,在项目根目录创建一个blade.config.php
文件,并在其中添加以下代码
<?php /** * Blade Config. * * @package wordpress-blade */ define( 'WORDPRESS_BLADE', [ 'paths_to_views' => [ __DIR__ . '/wp-content/themes/<your-theme>/<path-to-your-components>', // Any other paths where Blade needs to look for components. ], 'path_to_compiled_views' => __DIR__ . '/wp-content/themes/<your-theme>/dist/blade', // Where you want Blade to save compiled files. 'never_expire_cache' => false, // Use `true` on production environments. 'base_path' => __DIR__, // The base path that is common to the front-end and admin. ] );
启动布局。
作为最佳实践,并且如果适用,可以像这样启动整个布局
# bootstrap.blade.php
<x-layout>
<x-hello name="Jane">Hi there!</x-hello>
</x-layout>
# layout.blade.php
@php
get_header();
@endphp
<main>
{{ $slot }}
</main>
@php
get_footer();
@endphp
# hello.blade.php
@props( [
'name' => '',
] )
<div>
<h1>{{ $slot }}</h1>
<p>Hello, {{ $name }}</p>
</div>
然后在您的模板中加载视图
Travelopia\Blade\load_view( 'bootstrap' );
您也可以这样加载单个组件
$name = 'hello'; // The name of the component. $attributes = [ 'name' => 'Jane' ]; // Properties / attributes to pass into the component. $echo = true; // Whether to echo the component. `false` returns the component as a string. Travelopia\Blade\load_view( $name, $attributes, $echo );
这在您想从块和全站编辑加载组件时特别有用。