我正在使用 laravel 布局,并且有这样的设置;
// Controller
public function action_index()
{
$this->layout->nest('submodule', 'partials.stuff');
$this->layout->nest('content', 'home.index');
}
//布局
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
@yield('content');
</body>
</html>
//这是内容模板
@section('content')
<div>
@yield('submodule')
</div>
@endsection
我的问题是如何在“内容”部分插入部分模板?我还需要将变量传递给第二个模板“子模块”。
$this->layout->nest('partial', 'partials.partial');
这不起作用,因为它将 View 绑定(bind)到布局。而我需要将它绑定(bind)到“内容”模板中定义的部分。
有任何想法吗?
最佳答案
这是我修复 Laravel 嵌套 View 问题的方法:
使用此解决方案,您还可以将数据传递到主 View
解决方案:
您需要在 home/index.blade.php View 中渲染 partials.stuff,然后创建一个 View 以在 template.php 中渲染 'home/index.blade.php' 的 'content'
使用 <?php render('partials.stuff') ?>
首先制作你的 home/index.blade.php:
<div>
<?php render('partials.stuff') ?>
</div>
第二次渲染你的 View ——没有任何嵌套的“子模块”调用
public function action_index()
{
$this->layout->nest('content', View::make('home.index'),$data) ;
}
最后你的模板将保持不变——render
{{ $content }}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
{{ $content }}
</body>
</html>
希望这对您有所帮助,因为它解决了我的问题:)
关于php - Laravel 多个嵌套 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15125229/