Reputation: 356
I'm using a View to show my create form that its quite long.
Inside this form i'm using many times the same pieces of code which is mainly divs with class names and it looks like this.
<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>
I just want to change dynamically the First name: on my label and <input>
I've tried to reuse this code with @yield and @section but if it exists more than 1 times in the same file i get the same results as the first one.
<!-- 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')
Is there any Laravel way to approach this issue?
Upvotes: 2
Views: 1859
Reputation: 163748
You can use @include
directive and pass variables you want:
@include('form.view', ['firstName' => $var])
Also, as @Dmytrechko mentioned in his comment, you can use @each
directive if you want to iterate over an array or a collection:
@each('form.view', $names, 'firstName')
Then just move your code to new form.view
and use $firstName
variable as usual:
<label>First name: <span class="text-danger">{{ $firstName }}</span></label>
Upvotes: 2