Laravel - 在一个结构中调用多个模板

时间:2014-11-24 12:36:32

标签: php html laravel

我想在一个Master结构中添加Multiple模板。我尝试下面的代码,但它不起作用。我也在Stack中关注了很多问题,但没有找到正确的解决方案。如果你有解决方案与我分享。在此先感谢。

控制器:

class Harryandsally extends BaseController
{
    public function index()
    {
        return View::make('layouts.master');
    }
}

结构:(app \ view \ layouts \ master.blade.php)

<html>
<head>
    <title>Harry & Sally</title>    
</head>
<body>    
<div class="container">
    @yield('harry')
</div>
<div class="block1">
    @yield('sally')
</div>    
</body>
</html>

模板:(app \ view \ harry.blade)

@extends('layouts.master')
@section('harry')
<table>
    <tr>
        <td>Name</td>
        <td>Harry</td>
    </tr>
</table>
@stop

模板:(app \ view \ sally.blade)

@extends('layouts.master')
@section('sally')
<table>
    <tr>
        <td>Name</td>
        <td>Sally</td>
    </tr>
</table>
@stop

2 个答案:

答案 0 :(得分:1)

您是否想要包含Harry和Sally的观点? 如果是这种情况,您应该使用“include”=&gt; @include('harry')

以下是一个例子:

layouts.master:

<html>
<head>
    <title>Harry & Sally</title>    
</head>
<body>    
<div class="container">
    @include('harry')
</div>
<div class="block1">
    @include('sally')
</div>    
</body>
</html>

哈里:

<table>
    <tr>
        <td>Name</td>
        <td>Harry</td>
    </tr>
</table>

萨利:

<table>
    <tr>
        <td>Name</td>
        <td>Sally</td>
    </tr>
</table>

希望这有帮助。

答案 1 :(得分:1)

查看文档我会说你必须做以下事情:

class Harryandsally extends BaseController
{
 public function index()
 {
  return View::make('layouts.master')
   ->nest('child1', 'folder.harry')
   ->nest('child2', 'sally');
 }
}

在主刃:

<html>
 <head>
  <title>Harry & Sally</title>    
 </head>
 <body>    
  <div class="container">
   <?php echho isset($child1) ? $child1 : '' ?>
  </div>
  <div class="block1">
   <?php echho isset($child2) ? $child2 : '' ?>
  </div>    
 </body>
</html>

然后在你的观点中:

/views/folder/harry.blade.php:

<table>
 <tr>
  <td>Name</td>
  <td>Harry</td>
 </tr>
</table>

视图/ sally.blade.php:

<table>
 <tr>
  <td>Name</td>
  <td>Sally</td>
 </tr>
</table>

我还没有测试过。但我想,你可以更好地将围绕着孩子们的div移动到那些看法上。然后你可以在你的主人中使用这样的代码:

<?php
 $nr = 1;
 $child = 'child'.$nr;
 while(isset($$child){
  echo $$child;
  $nr++;
  $child = 'child' . $nr;
 }
?>

我还没有测试过,但我们的想法是你可以继续添加视图(child1,child2,child3),它会全部呈现。您也可以将数组中的所有子名称传递给master并循环遍历它。

希望它有所帮助。