6
votes

I'm getting an unexplained FatalErrorException when trying to implement a simple page layout using blade templating. I'm not sure if it's something I'm doing wrong or Laravel is. I'm following the tutorial on L4's documentation about Templating and my code seems to follow it. Here's my code.

app/routes.php:

<?php
Route::get('/', 'HomeController@showWelcome');

app/views/home/welcome.blade.php:

@extends('layouts.default')
@section('content')
  <h1>Hello World!</h1>
@stop

app/views/layouts/default.blade.php:

<!doctype html>
<html>
  <head>
    <title>The Big Bad Barn (2013)</title>
  </head>
  <body>
    <div>
      @yield('content')
    </div>
  </body>
</html>

app/controllers/HomeController.php:

<?php
class HomeController extends BaseController {
  protected $layout = 'layouts.default';
  public function showWelcome()
  {
    $this->layout->content = View::make('home.welcome');
  }
}

Laravel just throws a FatalErrorException. The output error page says "syntax error, unexpected '?'". The file blade is generating inside the storage/views directory has PHP where the

<?php echo $__env->make('layouts.default')

<?php $__env->startSection('content', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>; ?>
<h1>Hello World!</h1>
<?php $__env->stopSection(); ?>
3
Can you add a blank line after the @extends line? It looks like the ?> that should be on the first line has ended up on the 3rd line.Phill Sparks

3 Answers

8
votes

Yesterday I encountered the same problem you did, however the other answers didn't fix my problem. You've probably figured it out already, but maybe this post prevents others from spending as much time as I did figuring out what is wrong while it's such a small (but frustrating!) thing.

I'm using Notepad++ as text-editor and for some strange reason it had decided to use "MAC format" as the End-Of-Line (EOL) format. Apparently the Blade framework can't cope with that. Use the conversion function (in notepad++ : Edit -> EOL Conversion) to convert to Windows Format and it will work just fine..

1
votes

Your controller should be just returning View::make("home.welcome") instead of attaching it to the layout.

The welcome view then calls the layout so the controller is only concerned about the body template in this case.

Edit for showing example controller:

class HomeController extends BaseController {
  public function showWelcome()
  {
    return View::make('home.welcome');
  }
}
1
votes

Eric already gave the right answer, I just wanted to add that if you want to use

protected $layout = 'layouts.default';

in your HomeController, then leave showWelcome action intact and remove this line

@extends('layouts.default')

from welcome.blade.php file. That should work too.

Regards, Vlad