Laravel 4:带有数据的布局内的嵌套视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17550562/
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 08:06:59  来源:igfitidea点击:

Laravel 4: Nest view inside layout with data

phplaravellaravel-4laravel-routing

提问by Tom

I'm writing a simple app which only relies on a few routes and views. I've setup an overall layout and successfully nested a template using the following.

我正在编写一个简单的应用程序,它只依赖于一些路由和视图。我已经设置了一个整体布局并使用以下内容成功嵌套了一个模板。

routes.php

路由文件

View::name('layouts.master', 'master');
$layout = View::of('master');

Route::get('/users', function() use ($layout)
{
    $users = Users::all()
    return $layout->nest('content','list-template');
});

master.blade.php

master.blade.php

<h1>Template</h1>
<?=$content?>

list-template.php

列表模板.php

foreach($users as $user) {
   echo $user->title;
}

How do I pass the query results $usersinto my master template and then into list-temple.php?

如何将查询结果$users传递到我的主模板,然后传递到 list-temple.php?

Thanks

谢谢

回答by Laurence

->nestallows a 3rd argument for an array of data:

->nest允许数据数组的第三个参数:

   Route::get('/users', function() use ($layout)
    {
        $users = Users::all()
        return $layout->nest('content','list-template', array('users' => $users));
    });

Also in your master.blade.php file - change it to this:

同样在您的 master.blade.php 文件中 - 将其更改为:

<h1>Template</h1>
@yield('content')

list-template.blade.php <- note the blade filename:

list-template.blade.php <- 注意刀片文件名:

@extends('layouts.master')

@section('content')
<?php
  foreach($users as $user) {
     echo $user->title;
   }
?>
@stop