2016-04-04 118 views
0

我在我的刀片佈局文件和兩個表的刀片視圖中有代碼。有沒有辦法減少我重複的代碼量?以下是我的佈局文件中的代碼。 有沒有辦法讓佈局文件中的第一個表格,然後傳遞不同的變量,以在刀片視圖文件中創建多個不同的表格? 基本上,我可以使用佈局中第一張表的代碼重新創建刀片視圖文件中的第二張表嗎?laravel刀片減少代碼重複

<!DOCTYPE html> 
<html> 
<head> 
</head> 

<body> 
<table border = "1", cellspacing = "2", align = center> 
<tr> <td> <strong> ID </strong></td> <td><strong> Time </strong></td><td> <strong> Note </strong></td></tr> 
<!-- blade for loop --> 
@for ($i=0; $i <$countOfUniqueDate[$uniqueDates[0]]; $i++) 

<tr><td> {{ $number[$i] ->id }} </td><td> {{ $number[$i]->timevalue }} </td> <td> {{ $number[$i]->note }} </td></tr> 
@endfor 
</table> 

@yield('table') 

//I would like to get rid of the below code by reusing the above table code 
// to recreate the second table 

<br><br><br> 
<table border = "1", cellspacing = "2", align = center> 
<tr> <td> <strong> ID </strong></td> <td><strong> Time </strong></td><td> <strong> Note </strong></td></tr> 
@for($i= $countOfUniqueDate[$uniqueDates[0]]; $i < ($countOfUniqueDate[$uniqueDates[0]] + $countOfUniqueDate[$uniqueDates[1]]); $i++) 

<tr><td> {{ $number[$i] ->id }} </td><td> {{ $number[$i]->timevalue }} </td><td> {{ $number[$i]->note }} </td></tr> 
@endfor 

</table> 
<br><br><br><br><br> 

@yield('table2') 
</body> 

<br><br> 
</html> 

這是我的刀片視圖文件中的代碼。

<!DOCTYPE html> 
<html> 



@extends("layouts.practice") 
@section('table') 
@stop 


<p align="center"> 
<a href="https://villageprintwebapp.app/writeNote"> Add a comment </a> 
</p> 

</html> 

我也能夠在不引用section('table2')的情況下顯示這兩個表,但不知道這是如何工作的。

+1

我不明白。可以請更具體的你想達到什麼? – manix

+0

我不能明白 –

+0

有沒有辦法通過重用第一張表的代碼來製作第二張表? – ray

回答

1

對於任何代碼重複,你可以加入同在另一個刀片文件,包括相同的內,無論你使用相同的

@include(「視圖名」,$ params)方法

PARAMS將是一個數組,您想要傳遞到新刀片模板中特定代碼段的鍵值對

+0

我想要做這樣的事來創建第二個表。我有一個變量$ startCount和$ startCountArray在控制器中創建並傳遞給模板文件。在下面的代碼中,我試圖將這些變量重置爲不同的值以創建不同的表。 @include('layouts.practice',[「startCount」=> $ countOfUniqueDate [0],「startCountArray」 => $ countOfUniqueDate [1]]) – ray

+0

我得到這個工作後玩弄它。 @include('layouts.practice',[「startCount」=> $ countOfUniqueDate [0],「startCountArray」=> $ countOfUniqueDate [1]])。感謝您的幫助,我一直在努力 – ray