首页 > 解决方案 > 使 html 模板集中化

问题描述

在所有形式中,我在刀片中都有以下代码。

<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 行)?

标签: laravel-5.6laravel-5.7

解决方案


为什么不这样走:

步骤 1:创建一个名为partials

第 2 步:_form_field.blade.php在 partials 文件夹中创建

第 3 步:将以下代码复制并粘贴到新创建的文件中

<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>

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

主页.blade.php

@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

希望这可以帮助你。


推荐阅读