3

In my page controller I have

$this->layout->content = View::make('authentication/login')->with('page_title','title');

I am using a blade file for my template. In the head of the html, I have

<title>{{$page_title}}</title>

I get an error that $page_title is undefined.

What I want ideally is a $data=array('page_title'=>'Login','second_item'=>'value').... But as I cannot get the basic passing of a variable to a view working, I am sticking with that first of all.

4 Answers 4

3

There are many ways to achieve this, as @Gravy pointed out, but judging by the way she is trying to write the code, the solution would be:

$data = array();
$this->layout->with('data', $data);
$this->layout->content = View::make('home');

See more here: http://forums.laravel.io/viewtopic.php?pid=58548#p58548

Sign up to request clarification or add additional context in comments.

2 Comments

this works for me, thanks. I tried adding the line $this->layout->with($this->data); in the _construct method, so I didn't have to declare it in every controller, but it came back with an error - Call to a member function with() on a non-object. Inside the individual controller function it's fine though. Do you know why it doesn't see it as an object in the construct function?
I think you can use View::share() to pass data to the view from the constructor.
3
$data = 
[
    'page_title' => 'Login',
    'second_item' => 'value'
    ...
];

return View::make('authentication/login', $data);

// or

return View::make('authentication/login', compact('data'));

// or

return View::make('authentication/login')->with($data);

// or

return View::make('authentication/login')->with(['page_title' => 'Login', 'second_item' => 'value']);

// or

return View::make('authentication/login')->with(array('page_title' => 'Login', 'second_item' => 'value'));

2 Comments

return View::make('authentication/login')->with('data', $data);
You could do that @kaning but then you will need to access all elements within view from $data. e.g. {{ $data['page_title'] }}. Doing my 2nd option, you can just do this within view {{ $page_title }}
1
$data = array('page_title'=>'Login','second_item'=>'value');
return View::make('authentication/login', $data);

Comments

0

So to get layouts working in controllers you need to first declare the variable content in the layout blade template.

In your controller do what you have done already, but remember dot notation when working with directory structures in views. layouts.master is the same as layouts/master.blade.php.

class UserController extends BaseController {
    /**
     * The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    public function getIndex()
    {
        // Remember dot notation when building views
        $this->layout->content = View::make('authentication.login')
                                     ->with('page_title','title');
    }
}

in layouts/master.blade.php

<div class="content">
    {{-- This is the content variable used for the layout --}}
    {{ $content }}
</div>

in authentication/login.blade.php

<title>{{ $page_title }}</title>

This will work if you use this structure.

1 Comment

I get the $content variable echoing fine. It's the $page_title that doesn't echo. Also, why dot notation and not '/' between folders?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.