如何使用Laravel的Blade模板将变量传递到布局? [英] How do I pass a variable to the layout using Laravel' Blade templating?

查看:354
本文介绍了如何使用Laravel的Blade模板将变量传递到布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 4中,我的控制器使用Blade布局:

In Laravel 4, my controller uses a Blade layout:

class PagesController extends BaseController {
    protected $layout = 'layouts.master';
}

主版式已输出变量标题,然后显示视图:

The master layout has outputs the variable title and then displays a view:

...
<title>{{ $title }}</title>
...
@yield('content')
....

但是,在我的控制器中,我似乎只能将变量传递给子视图,而不是布局.例如,一个动作可以是:

However, in my controller I only appear to be able to pass variables to the subview, not the layout. For example, an action could be:

public function index()
{
    $this->layout->content = View::make('pages/index', array('title' => 'Home page'));
}

这只会将$title变量传递给视图的内容部分.如何为整个视图或至少在主布局中提供该变量?

This will only pass the $title variable to the content section of the view. How can I provide that variable to the whole view, or at the very least the master layout?

推荐答案

如果在内容布局中使用@extends,则可以使用以下方法:

If you're using @extends in your content layout you can use this:

@extends('master', ['title' => $title])

这篇关于如何使用Laravel的Blade模板将变量传递到布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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