代码之家  ›  专栏  ›  技术社区  ›  Pankaj

使html模板集中化

  •  0
  • Pankaj  · 技术社区  · 7 年前

    在所有的形式我有下面的代码在刀片。

    <div class="col-sm-6 col-md-6">
        <div class="form-group">
            <label class="control-label">Price</label>
            <input name="Price" type="number" class="form-control">
        </div>
    </div>
    

    我知道,通过使用 laravelcollective 上面的输入类型文本可以像下面这样处理。。

    Form::number('Price', '');
    

    有没有什么方法可以使它集中化,以避免为每个字段一次又一次地编写这个模板,这样就不必每次为每个数据库字段编写整个模板(html部分上方6行)?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Saiyan Prince    7 年前

    为什么不这样做:

    步骤1:创建一个名为 partials

    第二步:创建 _form_field.blade.php

    <div class="col-sm-6 col-md-6">
        <div class="form-group">
            <label class="control-label">{{ $labelName }}</label>
            <input name="{{ $input['name'] }}" type="{{ $input['type'] }}" class="{{ $input['class'] }}" />
        </div>
    </div>
    

    现在,您可以使用此文件并将其包含在视图中的任何位置。是这样的:

    @extends('your_main_layout_file_here')
    
    @section('content')
    
        <form action="{{ route('yourPostRoute') }}" method="POST">
            @csrf
    
            @include('partials._form_field', [
                'labelName' => 'Price',
                'input'     => [
                    'type'  => 'number',
                    'name'  => 'Price',
                    'class' => 'form-control'
                ]
            ])
        </form>
    
    @endsection
    

    希望这对你有帮助。

    推荐文章