elliotfleming
elliotfleming

Reputation: 43

How do inject data into a layout?

I am trying to inject data into a layout (not the sub-view), but I have not found any method that seems practical.

These are the two ways I currently know of to accomplish this. The data I will be injecting is the page title, for simplicity.

Method 1

// app/controllers/HomeController.php

protected $layout = 'main';

public function index()
{
    $this->layout->title = 'Page Title';
    $this->layout->content = View::make('home');
}


// app/views/layout.blade.php
...
<title>{{ $title }}</title>
...

Method 2

// app/views/home.blade.php
...
@section('title')
    Page Title
@stop
...


// app/views/layout.blade.php

...
<title>@yield('title')</title>
...

Once again, neither of these methods seem to be ideal, but I haven't seen a better method so far. I feel like Laravel must have some method built in to handle this...

Upvotes: 4

Views: 4083

Answers (3)

Carlos Arturo Alaniz
Carlos Arturo Alaniz

Reputation: 428

You could also use View's nest method...

Namespace: Illuminate\View

Located at Illuminate/View/View.php

nest( string $key, string $view, array $data = array() )
//Add a view instance to the view data.

layouts/bluePrint.blade.php

<html>
<div>
 <!--Some nested content goes here -->
{{$nested_content}}
</div>
</html>

forms/loginForm.blade.php

<h2>Login</h2>
{{Form::open(array
    ('route' => $form_route, 'method' => 'POST', 'role'=>'form'))}}

<div class="form-group">
    {{Form::label('username', 'Username', array
        ('class' => 'form-control'))}}
    {{Form::text('username', $value = NULL , array
        ('class' => 'form-control', 'placeholder'=>'Username'))}}
</div>
<div class="form-group">
    {{Form::label('password', 'Password', array
        ('class' => 'form-control'))}}
    {{Form::password('password' , array
        ('class' => 'form-control', 'placeholder'=>'Password'))}}
</div>
<div class="form-group">
    {{Form::submit('GO', array('class'=>'btn btn-default'))}}
</div>
{{Form::close()}}

routes.php

Route::get('test', array('as' => 'test', function() {
    $layout = View::make('layouts.blueprint');
    return $layout->nest('nested_content','forms.loginForm');
}));

Upvotes: 0

Jesper Lindstr&#246;m
Jesper Lindstr&#246;m

Reputation: 478

You can edit the setupLayout method in the BaseController to pass the data directly to the View::make call.

protected function setupLayout()
{
    if(!is_null($this->layout))
    {
        $data = array(
            'title' => 'Page Title'
        );

        $this->layout = View::make($this->layout, $data);
    }
}

Alternatively, you could change the setupLayout method to access $this->layoutData:

protected function setupLayout()
{
    if(!is_null($this->layout))
    {
        $this->layout = View::make($this->layout, $this->layoutData);
    }
}

Then set the following in your actual controller:

$this->layoutData = array('title' => 'Page Title');

Upvotes: 1

The Alpha
The Alpha

Reputation: 146191

Did you mean something like this ?

<!--app/views/layouts/master.blade.php-->

<html>
    <head><title>{{ $title }}</title></head>
    <body>
        <ul id="nav">
            @section('sidebar')
                <li>List Item One</li>
            @show
       </ul>

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

Controller:

public function showWelcome()
{
    $data['title'] = 'LaravelFour';
    $data['newItem'] = 'New List Item';
    return View::make('home', $data);
}

This is home.blade.php, notice @parent which will output <li>List Item One</li> and the variable {{ $newItem }} in the home.blade.php will be replaced with the value of $data['newItem'], which means New List Item

<!--app/views/home.blade.php-->

@extends('layouts.master')
@section('sidebar')
    @parent <!--This will represent <li>List Item One</li> -->
    <li>{{ $newItem }}</li> <!--This is new data, will be injected in the layout-->
@stop

@section('content')
    <p>This is my body content.</p>
@stop

The sidebar in the layout will be

<li>List Item One</li>
<li>New List Item</li>

I think, this way you are injecting new data in the master layout, is this what you are talking about. You may take a look at View Composers and this answer on SO too.

Upvotes: 0

Related Questions