laravel-5.6 - 使 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 行)?
解决方案
为什么不这样走:
步骤 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
希望这可以帮助你。
推荐阅读
- python - 我想删除最后一项 discord.py
- java - 如何告诉 Swagger V3 我的“Java 日期”只是一个“日期”,而不是“日期时间”?
- javascript - 将 node_module css 文件导入 NextJs
- android - 如何管理 html 字体大小,以便对 iOS 和 Android 进行相同的解析
- sql - 子查询中是否需要 FOR UPDATE?
- c# - 为什么当我通过 POST API 调用将时间发送到后端时,会额外增加 5 个小时?
- docker - 使用 DOCKER-USER 链 iptables 允许传出流量
- c - C语言求一组数的整数除数
- python - 为双指数曲线拟合找到 x?
- json - How to Parse JSON Arrays to SQL SERVER 2016