【问题标题】:laravel blade reducing code repetitionlaravel Blade 减少代码重复
【发布时间】:2016-07-24 11:27:11
【问题描述】:

我的刀片布局文件和刀片视图中有两个表格的代码。有没有办法减少我重复的代码量?下面是我的布局文件中的代码。 有没有办法让布局文件中的第一个表,然后传递不同的变量以在刀片视图文件中创建多个不同的表? 基本上我可以使用布局中第一个表的代码来重新创建刀片视图文件中的第二个表吗?

<!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') 的情况下显示两个表,但不确定它是如何工作的。

【问题讨论】:

  • 我不明白。能否更具体地说明您想要实现的目标?
  • 我也搞不定
  • 有没有办法通过重用第一个表的代码来制作第二个表?
  • 也许使用@include('table', $paramsAsArray) 而不是@yield
  • 这是你的答案吗? laravel.com/docs/5.2/blade#control-structures -- 向下滚动到“包含子视图”

标签: php laravel blade


【解决方案1】:

对于任何重复的代码,您都可以在另一个刀片文件中添加相同的代码,并在任何使用相同的地方包含相同的代码

@include('viewname',$params)

params 将是一个数组、键值对,您希望将其传递给新刀片模板中的特定代码段

【讨论】:

  • 我正在尝试做这样的事情来创建第二个表。我在控制器中创建了一个变量 $startCount 和 $startCountArray 并传递给模板文件。在下面的代码中,我试图将这些变量重置为不同的值以创建不同的表。 @include('layouts.practice', ["startCount" => $countOfUniqueDate[0], "startCountArray" => $countOfUniqueDate[1]])
  • 在玩过它之后我得到了这个工作。 @include('layouts.practice', ["startCount" => $countOfUniqueDate[0], "startCountArray" => $countOfUniqueDate[1]])。感谢您的帮助,我为此苦苦挣扎了一段时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多