0

I wanted to ask how can I define a multiple layouts for the same controller in Laravel. The scenario here is like the following:

I have a controller Home and i have two actions in this controller one called steps and the other called login.

I want the both of them load different layout.

The way that I used to make this is as follow:

protected $layout = "layouts.page";

public function index()
{
    // Get to the page of the website making steps
    $this->layout->content = View::make('steps');
}

Can I define multiple layouts? Maybe passing an array as follow:

protected $layout = array('first' => "layouts.page", 'second' => 'layouts.second');
4

6 回答 6

3

我是这样实现的

$this->layout = View::make('layout.master');
$this->layout->content = View::make('step.demo')
于 2015-03-30T04:56:29.243 回答
3

最好的解决方案是创建一种方法来生成您的视图,嵌套您的多个布局:

return View::make('layouts.master', array())
       ->nest('section_one', YOUR_SECOND_MASTER, array())
       ->nest...

并停止设置protected $layout布局。

于 2013-08-12T11:25:25.993 回答
2

使用View Composers或查看http://laravel.com/docs/responses#views下将子视图传递给视图的部分。

您还可以为http://laravel.com/docs/templates#blade-templating定义的布局指定多个部分

编辑:

如果要为来自同一控制器的不同视图定义主布局,请在 View it self 上定义布局。查看使用刀片布局部分

@extends用于定义视图本身的布局。

希望这对您正在寻找的内容有所帮助。

于 2013-08-12T11:03:56.723 回答
1

如果您查看控制器可能扩展的BaseController,您会看到布局变量最终被简单地用作任何旧视图的结果。

换句话说,您的$layout变量只是一个视图。您可以在控制器中创建任何$layout变量:

<?php

class MyController extends BaseController {

    protected $layout;

    protected $layout_alt;

    // Here we're over-riding setupLayout() from
    // the BaseController
    protected function setupLayout()
    {
        if ( ! is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
        }

        if ( ! is_null($this->layout_alt))
        {
            $this->layout_alt = View::make($this->layout_alt);
        }

    }

}

然后在您看来,您可以返回:

 $this->layout_alt->content = View::make('steps');

当然,正如 Abishek R Srikaanth 指出的那样,可能性是无穷无尽的。你也可以用 Blade 做一些花哨的事情 :D

于 2013-08-13T22:00:28.507 回答
1

我这样做的方式与@fideloper 的回答非常相似。

protected $layout;
private $_layout = null;

public function __construct()
{

}

private function _setupLayout()
{
    if ( ! is_null($this->_layout))
    {
        $this->layout = View::make($this->_layout);
    }
}

public function home() {
    $this->_layout = 'layouts.1col_public';
    $this->_setUpLayout();
    $this->layout->content = View::make('static/home');
}

public function about() {
    $this->_layout = 'layouts.2col_public';
    $this->_setUpLayout();
    $this->layout->active_menu = 'about';
    $this->layout->content = View::make('static/default');
}
于 2013-11-08T11:29:16.760 回答
0

这不是常见的做法,我还没有测试过,但值得一试。

在您的控制器的方法中:

$this->layout = View::make('layouts.master1");
于 2013-08-12T13:39:30.230 回答