PHP code example of isszz / think-blade

1. Go to this page and download the library: Download isszz/think-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/ */

    

isszz / think-blade example snippets




// view.php 模板配置, 多应用时, 每个应用的配置可以不同

return [
    // 模板引擎类型使用Blade
    'type' => 'Blade',
    // 模版主题
    'theme' => '',
    // 缓存路径
    'compiled' => '',
    // 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写 3 保持操作方法
    'auto_rule' => 1,
    // 视图目录名
    'view_dir_name' => 'view',
    // 模板起始路径
    'view_path' => '',
    // 模板后缀
    'view_suffix' => 'html.php',
    // 模板文件名分隔符
    'view_depr' => DIRECTORY_SEPARATOR,
    // 是否开启模板编译缓存,设为false则每次都会重新编译
    'tpl_cache' => true,
];

use think\facade\View;

View::directive('time2str', function($expression) {
  return " echo \Helper::time2str($expression); 

@time2str(time(), 'Y-m-d H:i')

use think\facade\View;

View::if('app', function (...$apps) {
    $appName = app('http')->getName();

    if (count($apps) > 0) {
        $patterns = is_array($apps[0]) ? $apps[0] : (array) $apps;
        return in_array($appName, $patterns);
    }

    return $appName;
});

@app('admin')
    // 后台应用
@elseapp('api')
    // api应用
@elseapp(['index', 'common'])
    // index和common应用
@else
    // 其他应用
@endapp

use think\facade\View;

/**
 * 认证
 */
class Auth
{
    protected $app;

    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;
    }

    /**
     * 初始化
     *
     * @param Request $request
     * @param Closure $next
     * @return Response
     */
    public function handle($request, Closure $next)
    {
        // 这个类自己实现, 需要用到的方法, 参见上面
        $auth = new \app\admin\service\Auth($this->app);

        // 容器注入
        $this->app->bind('auth', $auth);
        
        // 在线用户信息, 未登录返回guest用户
        $user = $auth->user();
        
        // 模版变量注入
        View::share([
            'auth' => $auth,
            'user' => $user,
        ]);
        
        return $next($request);
    }
}
html
// 多行php代码
@php
    $isActive = false;
    $hasError = true;
@endphp

<span @class([
    'p-4',
    'font-bold' => $isActive,
    'text-gray-500' => !$isActive,
    'bg-red' => $hasError,
])></span>

// 结果:
<span class="p-4 text-gray-500 bg-red"></span>
html
<form action="/foo/bar" method="POST">
    @method('PUT', '___method')
    ...
</form>