PHP code example of crazyfd / php-blade
1. Go to this page and download the library: Download crazyfd/php-blade library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
crazyfd / php-blade example snippets
echo view('homepage', ['name' => 'John Doe'])->render();
// 或直接输出(View 实现了 __toString)
echo view('homepage', ['name' => 'John Doe']);
blade(__DIR__ . '/views', __DIR__ . '/cache'); // 初始化(可选)
echo view('homepage', ['name' => 'John Doe']);
return [
'enable' => true,
// 编译后的视图缓存目录
'cache_path' => runtime_path() . '/views',
// 是否缓存编译结果(false 时每次重新编译,仅建议开发环境使用)
'cache' => true,
// 编译文件扩展名
'compiled_extension' => 'php',
// 检查缓存时间戳,模板更新后自动重新编译
'check_timestamps' => true,
];
use Jenssegers\Blade\Webman\View as BladeView;
return [
'handler' => BladeView::class,
];
return view('user/profile', ['name' => 'John Doe']);
use Jenssegers\Blade\Blade;
return [
'handler' => \Jenssegers\Blade\Webman\View::class,
'extension' => function (Blade $blade): void {
$blade->directive('shout', fn ($expression) => "<?= strtoupper((string) ($expression))
use Jenssegers\Blade\Blade;
$blade = new Blade('/path/to/views', '/path/to/cache');
echo $blade->render('homepage', ['name' => 'John Doe']);
$blade->make('view.name', $data)->render(); // 渲染视图
$blade->render('view.name', $data); // 等价快捷方式
$blade->exists('view.name'); // 视图是否存在
$blade->share('key', $value); // 共享数据
$blade->compiler(); // BladeCompiler 实例
$blade->directive('name', $handler); // 自定义指令
$blade->if('admin', $callback); // 自定义条件
$blade->component(Alert::class, 'alert'); // 注册组件
$blade->addNamespace('namespace', $path); // 命名空间视图
bash
composer bash
php webman blade:cache # 预编译所有 Blade 模板(上线前执行可提升首次访问速度)
php webman blade:clear # 清空编译缓存