【问题标题】:Laravel: Repeating the same HTML code in one blade.php fileLaravel:在一个 Blade.php 文件中重复相同的 HTML 代码
【发布时间】:2016-09-09 09:13:36
【问题描述】:

我正在使用视图来显示我的创建表单,它很长。

在这个表单中,我多次使用相同的代码,主要是带有类名的 div,看起来像这样。

<div class="row">
    <div class="col-md-6">
        <div class="form-group">
            <label>**First name:** <span class="text-danger">*</span></label>
            {{ Form::text('firstname',null,array('class' =>'form-control required','placeholder' =>'John'))}}
        </div>
    </div>
</div>

我只想动态更改标签上的 First name: 和 input>

我尝试使用 @yield 和 @section 重用这段代码,但如果它在同一个文件中存在超过 1 次,我会得到与第一个相同的结果。

<!-- First Name -->
@section('form_input_wrapper_label', 'First Name:')
@section('form_input_wrapper_input_area')
    {{ Form::text('firstname',null,array('class' =>'form-control required','placeholder' =>'John'))}}
@endsection
@include('theme_components.form_input_wrapper')

<!-- Last Name -->
@section('form_input_wrapper_label', 'Last Name:')
@section('form_input_wrapper_input_area')
    {{ Form::text('lastname',null,array('class' =>'form-control required','placeholder' =>'Smith'))}}
@endsection
@include('theme_components.form_input_wrapper')

Laravel 有什么方法可以解决这个问题吗?

【问题讨论】:

标签: php laravel laravel-4 laravel-5


【解决方案1】:

您可以使用@include 指令并传递您想要的变量:

@include('form.view', ['firstName' => $var])

另外,正如@Dmytrechko 在他的评论中提到的,如果你想遍历一个数组或一个集合,你可以使用@each 指令:

@each('form.view', $names, 'firstName')

然后只需将您的代码移动到新的form.view 并照常使用$firstName 变量:

<label>First name: <span class="text-danger">{{ $firstName }}</span></label>

【讨论】:

    猜你喜欢
    • 2012-06-29
    • 1970-01-01
    • 2023-03-17
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 2020-10-09
    • 2014-03-25
    相关资源
    最近更新 更多