sy-records/blade

一个独立的laravel视图模板 - blade版本

v7.0.3 2020-11-23 05:46 UTC

This package is auto-updated.

Last update: 2024-09-23 14:39:09 UTC


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及其作者。这是一个伟大的项目。