2016-09-09 7 views
1

私はかなり長い私の作成フォームを表示するためにビューを使用しています。Laravel:1つのblade.phpファイルに同じHTMLコードを繰り返します。

このフォームの中で、私は何度も同じコードを使用していますが、これは主にクラス名を持つ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> 

私はちょうど動的まず名前変更したい:私は@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') 

この問題を解決する方法はありますか?

+0

[@each( 'view.name'、$ jobs、 'job')](https://laravel.com/docs/5.3/blade#including-sub-views)を使用すると、 – Dmytrechko

答えて

2

あなたは@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> 
関連する問題