php – Laravel多个嵌套视图

前端之家收集整理的这篇文章主要介绍了php – Laravel多个嵌套视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用laravel布局,我有这样的设置;

//控制器

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');

这不起作用,因为它将视图绑定到布局.而我需要将它绑定到“内容”模板中定义的部分.

有任何想法吗?

解决方法

以下是我修复Laravel嵌套视图问题的方法

使用此解决方案,您还可以将数据传递到主视图

解:

您需要在home / index.blade.PHP视图中渲染partials.stuff,然后创建一个视图,在template.PHP中渲染’home / index.blade.PHP’的’content’

使用<?PHP render('partials.stuff')?>

首先制作你的home / index.blade.PHP

<div>
      <?PHP render('partials.stuff') ?>
</div>

第二次渲染你的视图 – 没有任何嵌套的“子模块”调用

public function action_index()
{
    $this->layout->nest('content',View::make('home.index'),$data) ;
}

最后,您的模板将保持不变 – 渲染{{$content}}

<!doctype html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    {{ $content }}
</body>
</html>

希望这可以帮助你解决我的问题:)

猜你在找的Laravel相关文章