Laravel 多个嵌套视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15125229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:35:11  来源:igfitidea点击:

Laravel multiple nested views

phppartial-viewslaravel

提问by Sinan

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

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

//Controller

//控制器

public function action_index()
{
    $this->layout->nest('submodule', 'partials.stuff');
    $this->layout->nest('content', 'home.index');
}

// layout

// 布局

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    @yield('content');
</body>
</html>

// this is the content template

// 这是内容模板

@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.

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

Any ideas?

有任何想法吗?

回答by deepika jain

Here is how I fixed the Laravel nested Views problem:

这是我修复 Laravel 嵌套视图问题的方法:

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

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

Solution:

解决方案:

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

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

Use <?php render('partials.stuff') ?>

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

First make your home/index.blade.php:

首先让你的 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) ;
}

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

最后你的模板将保持不变——渲染 {{ $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 :)

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

回答by Jürgen Paul

Here's what you would typically do:

以下是您通常会执行的操作:

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

And in your template:

在你的模板中:

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

and your home/index.blade.php:

和你的home/index.blade.php

<div>
     {{ $submodule }}
</div>