luoyy / blade
此包已被废弃,不再维护。未建议替代包。
laravel 视图模板的独立版本 - blade
7.0.2
2020-03-15 10:47 UTC
Requires
- php: ^7.2.5
README
这是一个从Laravel中提取出来的视图模板引擎,它是独立的,不依赖于Laravel的容器或其他任何东西。
安装
使用Composer,你只需要运行
composer require luoyy/blade
如果你还没有使用Composer,你应该将文件夹src
中的所有文件添加到你的项目文件夹中,然后在你的代码中require
它们。
如果你需要在php5上运行,切换到php5分支
使用
<?php require './vendor/autoload.php'; use luoyy\Blade\Compilers\BladeCompiler; use luoyy\Blade\Engines\CompilerEngine; use luoyy\Blade\Engines\EngineResolver; use luoyy\Blade\Engines\FileEngine; use luoyy\Blade\Engines\PhpEngine; use luoyy\Blade\Factory; use luoyy\Blade\Filesystem\Filesystem; use luoyy\Blade\FileViewFinder; $path = ['view_path']; // your view file path, it's an array $cachePath = '/cache_path'; // compiled file path $file = new Filesystem; $compiler = new BladeCompiler($file, $cachePath); // you can add a custom directive if you want $compiler->directive('datetime', function ($timestamp) { return preg_replace('/(\(\d+\))/', '<?php echo date("Y-m-d H:i:s", $1); ?>', $timestamp); }); $compiler->if('env', function ($test) { return $test == '123'; }); $compiler->component('components.alert', 'alert'); $resolver = new EngineResolver; $resolver->register('file', function () { return new FileEngine; }); $resolver->register('php', function () { return new PhpEngine; }); $resolver->register('blade', function () use ($compiler) { return new CompilerEngine($compiler); }); $finder = new FileViewFinder($file, $path); // get an instance of factory $factory = new Factory($resolver, $finder); // if your view file extension is not php or blade.php, use this to add it $factory->addExtension('tpl', 'blade'); // render the template file and echo it echo $factory->make('hello', ['a' => 1, 'b' => 2])->render(); ?>
你可以通过这个扩展享受几乎所有的blade特性。然而,请记住,一些独家特性已被移除。
你不能
- 在模板文件中使用
@inject
@can
@cannot
@lang
- 添加任何事件或中间件
文档:https://laravel.net.cn/docs/7.x/blade
感谢Laravel及其作者。这是一个伟大的项目。