Laravel 4:使用数据嵌套布局内的视图 [英] Laravel 4: Nest view inside layout with data

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

问题描述

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

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

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

list-template.php

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

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

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

谢谢

推荐答案

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

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

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

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

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

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

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

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

@extends('layouts.master')

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

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

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