我使用的是Laravel 4.2,我的页面上有多个引导框。我想把这些放在一个文件中,我可以包括一些参数,将插入到它,所以我不需要重复相同的代码为框每次。
控制器$box_options = array(
'icon_classes' => 'fa fa-pie-chart',
'box_title' => 'Browser Usage',
'box_footer_text' => 'View More Visitor Data'
);
return View::make('index')->with('box_options', $box_options);
box.blade.php (Include File)
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">
<i class="{{ $icon_classes }}"></i> {{ $box_title }}
</h3>
</div><!-- /.box-header -->
<div class="box-body">
<div class="row">
@include('includes.global.pie_chart', $browser_usage_pie_chart_options)
</div><!-- /.row -->
</div><!-- /.box-body -->
@if($box_footer_text)
<div class="box-footer text-center">
<a href="javascript::;" class="uppercase">{{ $box_footer_text }}</a>
</div><!-- /.box-footer -->
@endif
</div><!-- /.box -->
我需要框体的内容是动态的,所以它可以是一个字符串或另一个包含文件或两者的组合。如果它只是一个字符串,我可以很容易地将它与控制器一起传递,就像我与其他变量一样,但我需要它能够在那里放置一个包含文件,除了它可以包含许多不同的包含文件。
如何传递一个include作为另一个include的参数?还是有另一种完全不同的方法,我没有考虑过?
一个@include()
就像一个php的include "";
,所以变量的作用域不会改变,你可以在includes.global.pie_chart
中访问box.blade.php
的变量而不传递它。因为作用域不会改变,所以可以这样做:
return view('box', "browser_usage_pie_chart_options" => $browser_usage_pie_chart_options):
view box.blade.php have:
@include('includes.global.pie_chart')
和中包含.global。调用它:
{{ $browser_usage_pie_chart_options }}
我建议如果你的include被使用了很多次,有时变量不存在,在打印它之前检查它是否存在@if(isset($browser_usage_pie_chart_options))
像这样
@include("part.dateTemplate",["name" => "pr_date"])
// dateTemplate
{{ $name }} // pr_date