1 Star 1 Fork 0

屈超 / think-blade

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

blade

thinkphp8 blade view engine

Minimum PHP Version Minimum Thinkphp Version Stable Version Total Downloads License

安装

composer require isszz/think-blade

配置

<?php

// 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,
];

容器注入

容器中的类解析调用,对于已经绑定的类标识,会自动快速实例化

@inject('test', 'app\service\Test')

<div>{{ $test->info() }}</div>

扩展 Blade

Blade 允许你使用 directive 方法自定义指令。当 Blade 编译器遇到自定义指令时,这会调用该指令包含的表达式提供的回调。

use think\facade\View;

View::directive('time2str', function($expression) {
  return "<?php echo \Helper::time2str($expression); ?>";
});

用法, 当然你也可以传递参数

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

自定义 If 语句

在定义简单的、自定义条件语句时,编写自定义指令比必须的步骤复杂。在这种情况下,think Blade 提供了 View::if 方法,它允许你使用闭包快速度定义条件指令。例如,定义一个校验当前应用的自定义指令

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

需要使用到 auth 和 权限验证时, 需要自行实现一个 auth 挂载进 app 且实现下列方法

 * @method auth->check 判断当前用户是否登录
 * @method auth->guest 判断当前用户是否为游客

 * @method auth->can 用户是否有权限
 * @method auth->cannot 用户不能执行这个权限
 * @method auth->any 用户是否具有来自给定能力列表的任何授权能力

中间件 挂载 auth 到 app 案例

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);
    }
}

有条件地编译 class 样式

@class指令有条件地编译 CSS class 样式。该指令接收一个数组,其中数组的键包含你希望添加的一个或多个样式的类名,而值是一个布尔表达式。如果数组元素有一个数值的键,它将始终包含在呈现的 class 列表中:

// 多行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>

同样,@style 指令可用于有条件地将内联 CSS 样式添加到一个 HTML 元素中。

// 单行php代码可以简写如下
@php($isActive = true)

<span @style([
    'background-color: red',
    'font-weight: bold' => $isActive,
])></span>

// 结果:
<span style="background-color: red; font-weight: bold;"></span>

附加属性

为方便起见,你可以使用该@checked指令轻松判断给定的 HTML 复选框输入是否被「选中(checked)」。如果提供的条件判断为true,则此指令将回显checked

<input type="checkbox" name="active" value="active" @checked(true) />

@selected指令可用于判断给定的选项是否被「选中(selected)」:

@php
$versions = [
    '1.0',
    '1.2',
    '1.3',
    '1.4',
    '1.5',
];
@endphp
<select name="version">
    @foreach ($versions as $version)
        <option value="{{ $version }}" @selected('1.2' == $version)>
            {{ $version }}
        </option>
    @endforeach
</select>

@disabled指令可用于判断给定元素是否为「禁用(disabled)」:

<button type="submit" @disabled($errors->isNotEmpty())>Submit</button>

@readonly指令可以用来指示某个元素是否应该是「只读 (readonly)」的。

<input type="email" name="email" value="email@laravel.com"  @readonly(1) />

@required指令可以用来指示一个给定的元素是否应该是「必需的(required)」。

<input type="text" name="title" value="title" @required(1) />

表单csrf token

@csrf
// 也支持参数, 同thinkphp的Request::buildToken()参数相同
@csrf($name, $type)
// 结果:
<input type="hidden" name="__token__" value="a47f4452e760ae12af62c11fbea5c65e">

Method 字段

由于 HTML 表单不能发出 PUTPATCHDELETE 请求,因此需要添加一个隐藏的 __method__ 字段来欺骗这些 HTTP 动词。 @method Blade 指令可以为你创建此字段:

<form action="/foo/bar" method="POST">
    @method('PUT', '___method')
    ...
</form>

自定义button组件

<!-- /app/index/view/default/components/button.html.php -->
<button type="{{ $type }}" {{ $attributes->whereDoesntStartWith('name') }}>{{ $name }}</button>

// 引用
<x-button type="submit" name="提交" class="btn btn-submit" />

自定义button组件

<!-- /app/index/view/default/components/alert.html.php -->
<div class="alert">
    <h3 {{ $title->attributes }}>{{ $title ?? '' }}</h3>
    {{ $slot }}
</div>

// 引用
<x-alert type="info" class="mb-4">
    <x-slot:title class="color-red">
        Server Error
    </x-slot>
    <strong>Whoops!</strong> Something went wrong!
</x-alert>

更多用法参考 laravel blade 手册

https://learnku.com/docs/laravel/10.x/blade/14852

The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

thinkphp8 blade view engine 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/isszz/think-blade.git
git@gitee.com:isszz/think-blade.git
isszz
think-blade
think-blade
master

搜索帮助