Laravel多个嵌套视图 [英] Laravel multiple nested views

查看:360
本文介绍了Laravel多个嵌套视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel布局,并且具有这样的设置;

I'm using laravel layouts and I have a setup like this;

//控制器

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

我的问题是如何在内容"部分中插入部分模板?我还需要将变量传递给第二个模板子模块".

My question is how can I insert a partial template inside the 'content' section? I also need to pass variables to this second template "submodule".

$this->layout->nest('partial', 'partials.partial');

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

This doesn't work because it binds the view to layout. Whereas I need to bind it to a section which is defined in the "content" template.

有什么想法吗?

推荐答案

这是我解决Laravel嵌套视图问题的方法:

Here is how I fixed the Laravel nested Views problem:

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

Using this solution you pass data to your main View as well

解决方案:

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

You need to render the partials.stuff inside your home/index.blade.php view and then make a view to render 'content' of 'home/index.blade.php' in your template.php

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

首先创建您的home/index.blade.php:

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

第二次呈现您的视图-无需任何嵌套的子模块"调用

Second render your view -- without any nested 'submodule' call

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

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

Finally Your template will remain same -- render {{ $content }}

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

希望这对您有所帮助,因为它解决了我的问题:)

Hope this helps you as it has solved my problem :)

这篇关于Laravel多个嵌套视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆