junichimura/view-args

v0.0.4 2019-10-31 05:43 UTC

This package is auto-updated.

Last update: 2024-09-29 05:02:46 UTC


README

描述

此包仅适用于Laravel。
您可以将对象指定为视图参数。
帮助您摆脱视图参数的困境。

安装

Composer

  • 对于Laravel:在您的项目文件夹中运行 composer require junichimura/view-args

使用示例 [仅适用于Laravel]

示例 1:View-Args 类

<?php

use Junichimura\ViewArgs\FormArgs;

class InputFormArgs extends FormArgs
{

    public $label;
    public $type;
    public $disabled;

    public function __construct($label, $type)
    {
        $this->label = $label;
        $this->type = $type;
    }

    public function disabled($condition = true)
    {
        $this->disabled = (bool)$condition;
        return $this;
    }

    public static function make($label, $type = 'text')
    {
        return new static($label, $type);
    }

}

示例 2:Blade 模板

{{-- views/sample/input.blade.php --}}
<div class="form-group">
    <label>{{ $args['label'] }}</label>
    <input class="form-group" name="{{ $args->name }}" type="{{ $args->type }}"
            @isset( $args->value ) value="{{ $args->value }}" @endisset
            placeholder="{{ $args->placeholder }}"
            @if($args->disabled) disabled @endif>
    @error( $args->name )
    <div class="alert alert-danger">{{ $message }}</div>
    @enderror
</div>

示例 3:渲染blade模板

<?php
// routes/web.php
Route::get('sample', function () {
    return view('sample.input', \InputFormArgs::make('Your Email', 'email')
                                                ->value('junichimura@examle.org')
                                                ->placeholder('your@email.org')
                                                ->disabled());
});

结果:生成的html

<div class="form-group">
    <label>Your Email</label>
    <input class="form-group" name="" type="email" value="junichimura@examle.org" placeholder="your@email.org" disabled >
</div>

许可协议

Laravel-View-Args 是开源软件,许可协议为 MIT 协议